paper.js/examples/Scripts/DivisionRaster.html

89 lines
34 KiB
HTML
Raw Normal View History

2011-06-18 21:32:07 -04:00
<!-- <!DOCTYPE html> -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title>
<link rel="stylesheet" href="../css/style.css">
<script type="text/javascript" src="../../dist/paper.js"></script>
<script type="text/paperscript" canvas="canvas">
// Based on 'JPEG Raster' by Jonathan Puckey:
// http://www.flickr.com/photos/puckey/3179779686/in/photostream/
2011-06-19 09:01:53 -04:00
2011-06-18 21:32:07 -04:00
// Create a raster item using the image with id='lenna'
var raster = new Raster('lenna');
// Make the raster invisible:
raster.visible = false;
2011-06-19 09:01:53 -04:00
// Create a seperate layer that the paths will be placed on:
var layer = new Layer();
// The mouse has to drag at least 5pt until the next onMouseDrag
2011-06-18 21:32:07 -04:00
// event is fired:
tool.minDistance = 5;
2011-06-19 09:01:53 -04:00
2011-06-18 21:32:07 -04:00
function onMouseMove(event) {
// Each drag event, iterate through the children of group:
2011-06-19 09:01:53 -04:00
for (var i = 0; i < layer.children.length; i++) {
var child = layer.children[i];
2011-06-18 21:32:07 -04:00
var bounds = child.bounds;
2011-06-19 09:01:53 -04:00
2011-06-18 21:32:07 -04:00
if (bounds.contains(event.point)) {
// If the mouse position intersects with the bounding
// box of the path, we're going to split it into two paths:
2011-06-19 09:01:53 -04:00
2011-06-18 21:32:07 -04:00
var size = bounds.size;
var isLandscape = size.width > size.height;
2011-06-19 09:01:53 -04:00
2011-06-18 21:32:07 -04:00
// If the path is in landscape orientation, we're going to
// split the path horizontally, otherwise vertically:
if (isLandscape) {
size.width /= 2;
} else {
size.height /= 2;
}
2011-06-19 09:01:53 -04:00
2011-06-18 21:32:07 -04:00
var topLeft = bounds.topLeft.floor();
var path = new Path.Rectangle(topLeft, size.ceil());
path.fillColor = raster.getAverageColor(path);
path.moveBelow(child);
2011-06-19 09:01:53 -04:00
2011-06-18 21:32:07 -04:00
var secondPath = path.clone();
size = size.floor();
secondPath.position += isLandscape
? [size.width, 0]
: [0, size.height];
secondPath.fillColor = raster.getAverageColor(secondPath);
secondPath.moveBelow(path);
2011-06-19 09:01:53 -04:00
2011-06-18 21:32:07 -04:00
// Remove the path which was split:
child.remove();
2011-06-19 09:01:53 -04:00
2011-06-18 21:32:07 -04:00
// Avoid continuing looping through the rest of the items:
return;
}
}
}
2011-06-19 09:01:53 -04:00
function onResize(event) {
layer.removeChildren();
// Transform the raster so that it fills the bounding rectangle
// of the view:
raster.fitBounds(view.bounds, true);
// Create a path that fills the view, and fill it with
// the average color of the raster:
var path = new Path.Rectangle(view.bounds);
path.fillColor = raster.getAverageColor(path);
}
// Call onResize directly:
onResize();
2011-06-18 21:32:07 -04:00
</script>
</head>
<body>
<canvas id="canvas" resize keepalive="true"></canvas>
<img width="512" height="512" id="lenna" style="display: none;" src="data:image/jpeg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAAXAAD/4QMraHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjAtYzA2MCA2MS4xMzQ3NzcsIDIwMTAvMDIvMTItMTc6MzI6MDAgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvY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