Skip to content

Instantly share code, notes, and snippets.

@notlion
Created June 17, 2011 23:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notlion/1032614 to your computer and use it in GitHub Desktop.
Save notlion/1032614 to your computer and use it in GitHub Desktop.
Messy Circle Drawer
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();
}
@notlion
Copy link
Author

notlion commented Jun 17, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment