Created
June 17, 2011 23:55
-
-
Save notlion/1032614 to your computer and use it in GitHub Desktop.
Messy Circle Drawer
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 plask = require('plask'); | |
plask.SkPath.prototype.splineTo = function(vertices){ | |
var nv = vertices.length; | |
if(nv >= 3){ | |
var p0, p1, p2; | |
for(var i = 1, n = nv - 1; i <= n; i++){ | |
p0 = vertices[i - 1]; | |
p1 = vertices[i]; | |
if(i == n){ // finish him | |
this.cubicTo( | |
(2 * p0.x + p1.x) / 3, (2 * p0.y + p1.y) / 3, | |
(2 * p1.x + p0.x) / 3, (2 * p1.y + p0.y) / 3, | |
p1.x, p1.y | |
); | |
} | |
else{ | |
p2 = vertices[i + 1]; | |
this.cubicTo( | |
(2 * p0.x + p1.x) / 3, (2 * p0.y + p1.y) / 3, | |
(2 * p1.x + p0.x) / 3, (2 * p1.y + p0.y) / 3, | |
(p0.x + 4 * p1.x + p2.x) / 6, (p0.y + 4 * p1.y + p2.y) / 6 | |
); | |
} | |
} | |
} | |
} | |
var _width = 500; | |
var _height = 500; | |
var window = plask.simpleWindow({ | |
settings: { | |
type: '2d', | |
title: 'hello pdf', | |
width: _width, | |
height: _height | |
}, | |
init: function(){ | |
var canvas = this.canvas, paint = this.paint; | |
paint.setFlags(paint.kAntiAliasFlag); | |
this.on('keyDown', function(e){ | |
switch(e.str){ | |
case ' ': | |
render(canvas, paint); | |
window.redraw(); | |
break; | |
case 's': | |
var pdf = plask.SkCanvas.createForPDF(canvas.width, canvas.height); | |
render(pdf, paint); | |
pdf.writePDF("hello.pdf"); | |
break; | |
} | |
}); | |
render(canvas, paint); | |
} | |
}); | |
function rand2(min, max){ | |
return min + Math.random() * (max - min); | |
} | |
function render(canvas, paint){ | |
canvas.drawColor(255,255,255); | |
paint.setStyle(paint.kStrokeStyle); | |
var verts = []; | |
var radius = Math.min(_width, _height) * 0.3; | |
var revolutions = rand2(1, 10); | |
var facets = rand2(4, 8); | |
var messyness = radius / facets * 2; | |
for(var i = 0, n = facets * revolutions; i < n; ++i){ | |
var t = i / (n - 1) * Math.PI * 2 * revolutions; | |
verts.push({ | |
x: Math.sin(t) * radius + rand2(-messyness, messyness), | |
y: Math.cos(t) * radius + rand2(-messyness, messyness) | |
}) | |
} | |
canvas.save(); | |
canvas.translate(_width / 2, _height / 2); | |
var guide = new plask.SkPath(); | |
guide.moveTo(verts[0].x, verts[0].y); | |
for(var i = 1; i < verts.length; ++i) | |
guide.lineTo(verts[i].x, verts[i].y); | |
paint.setColor(230,230,230); | |
paint.setStrokeWidth(2); | |
canvas.drawPath(paint, guide); | |
var path = new plask.SkPath(); | |
path.moveTo(verts[0].x, verts[0].y); | |
path.splineTo(verts); | |
paint.setColor(255,0,0); | |
paint.setStrokeWidth(5); | |
canvas.drawPath(paint, path); | |
canvas.restore(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://f.cl.ly/items/2B382Z1i180P2N0T043k/hello.pdf