You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.9 KiB

6 years ago
<title>JavaScript <=> PaperScript example</title>
<code mode="text/html">
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="paper.js"></script>
<script type="text/paperscript" canvas="canvas">
var xstart = Math.random();
var ystart = Math.random();
//var xstart = 0.976119763841021;
//var ystart = 0.06095897568349018;
//var xstart = 0.981709002734148;
//var ystart = 0.7482912451397156;
//var xstart = 0.01408766142248663
//var ystart = 0.5826524694499656;
var scale = 500;
var num_steps = 7;
var fac = 0.0005;
var num_steps = 5;
var fac = 0.05;
var lines = [];
for(var i=0; i<2; i++) {
var x = xstart + i*fac;
var y = ystart + i*fac;
var path = new Path({strokeWidth: 1, strokeColor: 'black'});
for(var s=0; s<num_steps; s++) {
x = (4*x*(1-x)) % 1;
y = (x+y) % 1;
var p = new Point(x*scale, y*scale);
path.add(p);
console.log(p);
}
lines.push(path);
}
lines[0].strokeWidth = 5;
lines[1].strokeWidth = 0;
//lines[1].fullySelected = true;
for(var i=0; i<num_steps-1; i++) {
var o0start = lines[0].getOffsetOf(lines[0].segments[i].point);
var o0end = lines[0].getOffsetOf(lines[0].segments[i+1].point);
var o1start = lines[1].getOffsetOf(lines[1].segments[i].point);
var o1end = lines[1].getOffsetOf(lines[1].segments[i+1].point);
var num_steps2_0 = (o0end - o0start) / 7;
var num_steps2_1 = (o1end - o1start) / 7;
var num_steps2 = Math.min(num_steps2_0, num_steps2_1);
console.log(num_steps2);
for(var j=0; j<num_steps2; j++) {
var p0 = lines[0].getPointAt(o0start + (o0end-o0start) * (j/num_steps2));
var p1 = lines[1].getPointAt(o1start + (o1end-o1start) * (j/num_steps2));
var path = new Path.Line({
from: p0, to: p1,
strokeWidth: 2,
strokeColor: 'black'
});
if(path.length < 5) {
path.removeSegments();
delete path;
}
}
}
globals.get_svg = function() {
return project.exportSVG({asString: true});
}
console.log("start: ", xstart, ystart);
</script>
<script type="text/javascript">
//document.addEventListener('DOMContentLoaded', function() {
window.globals = {}
window.onload = function() {
/*
draw();
document.getElementById('redraw').addEventListener('click', function() {
draw();
});
*/
paper.install(window);
console.log(window.globals);
document.getElementById('download-svg').addEventListener('click', function() {
var svgdata = 'data:image/svg+xml;utf8,' + encodeURIComponent(window.globals.get_svg());
var a = document.createElement('a');
a.download = 'export.svg';
a.href = svgdata;
document.body.appendChild(a);
a.click();
});
}
</script>
</head>
<body>
<button id="download-svg">download svg</button>
<canvas id="canvas" style="width: 500px; height: 500px;" resize></canvas>
</body>
</html>