paper.js/examples/Paperjs.org/SpiralRaster.html

121 lines
17 KiB
HTML
Raw Normal View History

2011-07-05 12:28:30 -04:00
<!DOCTYPE html>
<html>
<head>
2014-08-16 13:24:54 -04:00
<meta charset="UTF-8">
<title>Spiral Raster</title>
<link rel="stylesheet" href="../css/style.css">
<script type="text/javascript" src="../../dist/paper-full.js"></script>
<script type="text/paperscript" canvas="canvas">
// Please note: dragging and dropping images only works for
// certain browsers when serving this script online:
var path, position, max;
var count = 0;
var grow = false;
// As the web is asynchronous, we need to wait for the raster to
// load before we can perform any operation on its pixels.
2014-08-16 13:24:54 -04:00
var raster = new Raster('mona');
raster.visible = false;
raster.on('load', resetSpiral);
2014-08-16 13:24:54 -04:00
var text = new PointText({
justification: 'right',
fontSize: 12,
content: window.FileReader
? 'drag & drop an image from your desktop to rasterize it'
: 'to drag & drop images, please use Webkit, Firefox, Chrome or IE 10'
});
function onFrame(event) {
if (grow) {
if (raster.loaded && (view.center - position).length < max) {
2014-08-16 13:24:54 -04:00
for (var i = 0, l = count / 36 + 1; i < l; i++) {
growSpiral();
}
path.smooth();
} else {
grow = false;
}
}
}
function growSpiral() {
count++;
var vector = new Point({
angle: count * 5,
length: count / 100
});
var rot = vector.rotate(90);
var color = raster.getAverageColor(position + vector / 2);
var value = color ? (1 - color.gray) * 3.7 : 0;
rot.length = Math.max(value, 0.2);
path.add(position + vector - rot);
path.insert(0, position + vector + rot);
position += vector;
}
function resetSpiral() {
grow = true;
// Transform the raster, so it fills the view:
raster.fitBounds(view.bounds);
if (path)
path.remove();
position = view.center;
count = 0;
path = new Path({
fillColor: 'black',
closed: true
});
position = view.center;
max = Math.min(raster.bounds.width, raster.bounds.height) * 0.5;
}
function onResize() {
if (raster.loaded)
resetSpiral();
2014-08-16 13:24:54 -04:00
text.point = view.bounds.bottomRight - [30, 30];
}
function onKeyDown(event) {
if (event.key == 'space') {
path.selected = !path.selected;
}
}
function onDocumentDrag(event) {
event.preventDefault();
}
function onDocumentDrop(event) {
event.preventDefault();
var file = event.dataTransfer.files[0];
var reader = new FileReader();
reader.onload = function (event) {
2014-08-16 13:24:54 -04:00
var image = document.createElement('img');
image.onload = function () {
raster = new Raster(image);
raster.visible = false;
2014-08-16 13:24:54 -04:00
resetSpiral();
};
image.src = event.target.result;
};
reader.readAsDataURL(file);
}
document.addEventListener('drop', onDocumentDrop, false);
document.addEventListener('dragover', onDocumentDrag, false);
document.addEventListener('dragleave', onDocumentDrag, false);
2014-08-16 13:24:54 -04:00
</script>
2011-07-05 12:28:30 -04:00
</head>
<body>
2014-08-16 13:24:54 -04:00
<canvas id="canvas" resize style="background-color:white"></canvas>
<img width="512" height="512" id="mona" style="display: none;" src="data:image/jpeg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAATAAD/4QMraHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjAtYzA2MCA2MS4xMzQ3NzcsIDIwMTAvMDIvMTItMTc6MzI6MDAgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bXA6Q3JlYXRvclRvb2w9IkFkb2JlIFBob3Rvc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