Using lines in Canvas
Created
January 24, 2015 13:43
-
-
Save tagr/4bc746997cee537bb58a to your computer and use it in GitHub Desktop.
HTML Canvas Line Demo
This file contains 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
canvas(id='c',width='800',height='600') |
This file contains 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
var CanvasPen = function(options){ | |
var doc = document, | |
canvas = doc.querySelector('#c'), | |
canvasH = canvas.height, | |
canvasW = canvas.width, | |
context = canvas.getContext('2d'), | |
randomize = function(seed) { | |
return Math.floor(Math.random()*seed); | |
}, | |
drawLine = function(points) { | |
context.lineWidth = 1; | |
context.strokeStyle = 'rgba('+randomize(196)+','+randomize(0)+','+(randomize(128)+127)+','+Math.random()+')'; | |
context.beginPath(); | |
context.moveTo(points.x1,points.y1); | |
context.lineTo(points.x2,points.y2); | |
context.stroke(); | |
context.closePath(); | |
}, | |
setup = function() { | |
context.lineCap = 'square'; | |
return true; | |
}, | |
clear = function() { | |
context.clearRect(0, 0, canvasW, canvasH); | |
return 0; | |
}, | |
start = options.init ? setup() : false, | |
LIMIT = 4096, | |
counter = 0; | |
setInterval(function(){ | |
counter = (counter > LIMIT) ? clear() : counter + 1; | |
drawLine({ | |
x1:Math.round(Math.random())*canvasW, | |
y1:Math.round(Math.random())*canvasH, | |
x2:randomize(canvasW), | |
y2:randomize(canvasH) | |
}); | |
}, 1); | |
}({init:true}); |
This file contains 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
body { | |
background: #000; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment