HTML Canvas Image Move Color Pixel
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Pixel Move</title> </head>/*from w w w. ja v a 2 s .c o m*/ <body> <canvas id="canvas" width="400" height="400"></canvas> <aside>Move mouse on canvas element.</aside> <script> captureMouse = function (element) { var mouse = {x: 0, y: 0, event: null}, body_scrollLeft = document.body.scrollLeft, element_scrollLeft = document.documentElement.scrollLeft, body_scrollTop = document.body.scrollTop, element_scrollTop = document.documentElement.scrollTop, offsetLeft = element.offsetLeft, offsetTop = element.offsetTop; element.addEventListener('mousemove', function (event) { var x, y; if (event.pageX || event.pageY) { x = event.pageX; y = event.pageY; } else { x = event.clientX + body_scrollLeft + element_scrollLeft; y = event.clientY + body_scrollTop + element_scrollTop; } x -= offsetLeft; y -= offsetTop; mouse.x = x; mouse.y = y; mouse.event = event; }, false); return mouse; }; window.onload = function () { var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'), mouse = captureMouse(canvas); (function drawFrame () { window.requestAnimationFrame(drawFrame, canvas); //draw some stripes: red, green, and blue for (var i = 0; i < canvas.width; i += 10) { for (var j = 0; j < canvas.height; j += 10) { context.fillStyle = (i % 20 === 0) ? "red" : ((i % 30 === 0) ? "blue" : "pink"); context.fillRect(i, j, 10, 10); } } var imagedata = context.getImageData(0, 0, canvas.width, canvas.height), pixels = imagedata.data; //pixel iteration for (var y = 0; y < imagedata.height; y += 1) { for (var x = 0; x < imagedata.width; x += 1) { var dx = x - mouse.x, dy = y - mouse.y, dist = Math.sqrt(dx * dx + dy * dy), offset = (x + y * imagedata.width) * 4, r = pixels[offset], g = pixels[offset + 1], b = pixels[offset + 2]; pixels[offset] = Math.cos(r * dist * 0.01) * 256; pixels[offset + 1] = Math.sin(g * dist * 0.01) * 256; pixels[offset + 2] = Math.cos(b * dist * 0.005) * 256; } } context.putImageData(imagedata, 0, 0); }()); }; </script> </body> </html>