Javascript examples for Canvas:Example
Get background color of input range position
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script> <style id="compiled-css" type="text/css"> #canvas{border:1px solid red;} #myRange{width:300px;} </style> <script type="text/javascript"> $(window).load(function(){/*from w w w . j a v a 2s . co m*/ var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.font = "14px verdana"; var $canvas = $("#canvas"); var canvasOffset = $canvas.offset(); var offsetX = canvasOffset.left; var offsetY = canvasOffset.top; var x1 = 0; var y1 = 10; var x2 = canvas.width; var y2 = 10; // create a gradient var gradient = ctx.createLinearGradient(x1, y1, x2, y2); gradient.addColorStop(0.00, "indigo"); gradient.addColorStop(1.00, "steelblue"); ctx.fillStyle = gradient; ctx.fillRect(x1, y1 - 10, x2 - x1, 20); var data = ctx.getImageData(x1, y1, canvas.width, 1).data; $("#myRange").on("input change", function () { var value = $(this).val(); var i = value * 4; var r = data[i]; var g = data[i + 1]; var b = data[i + 2]; var a = data[i + 3]; var fill = "rgba(" + r + "," + g + "," + b + "," + a + ")"; ctx.fillStyle = fill; ctx.clearRect(0, 20, canvas.width, 30); ctx.fillRect(0, 25, 50, 20); ctx.fillText(fill, 75, 40); }); }); </script> </head> <body> <input id="myRange" type="range" min="0" max="299"> <br> <canvas id="canvas" width="300" height="45"></canvas> </body> </html>