Last active
December 20, 2015 06:49
-
-
Save vladkens/6088711 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <meta charset="utf-8" /> | |
| <style> | |
| #canvas { | |
| display: block; | |
| margin: 0 auto; | |
| border: 1px solid black; | |
| } | |
| </style> | |
| <canvas id="canvas"></canvas> | |
| <script> | |
| (function(width, height, v) { | |
| width = width || 640; | |
| height = height || 480; | |
| v = v || (width / 200); | |
| var canvas, ctx, blockSize = height / 40; | |
| var points = [], add = null; | |
| window.onload = function() { | |
| canvas = document.getElementById('canvas'); | |
| canvas.width = width; | |
| canvas.height = height; | |
| ctx = canvas.getContext('2d'); | |
| canvas.addEventListener('click', function(event) { | |
| add = {x: 0, y: Math.floor(event.y / blockSize) * blockSize, | |
| color: 'rgb('+rand(255)+','+rand(255)+','+rand(255)+')'}; | |
| }, true); | |
| render(); | |
| } | |
| function render() { | |
| requestAnimationFrame(render); | |
| if (add !== null) { | |
| points.push(add); | |
| add = null; | |
| } | |
| for (var i in points) { | |
| var w = Math.floor(points[i].x / blockSize), | |
| h = Math.floor(Math.max(height - points[i].y, points[i].y) / blockSize); | |
| if (w - h > Math.ceil(width / blockSize)) { | |
| points.splice(i, 1); | |
| continue; | |
| } | |
| points[i].x += v; | |
| ctx.fillStyle = points[i].color; | |
| for (var j = w; j >= 0; --j) { | |
| ctx.fillRect(0, points[i].y - ((w - j) * blockSize), j * blockSize, blockSize); | |
| ctx.fillRect(0, points[i].y + ((w - j) * blockSize), j * blockSize, blockSize); | |
| } | |
| } | |
| } | |
| function rand(num) { | |
| return Math.floor(Math.random() * num); | |
| } | |
| })(600, 600, 5); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment