oxjs/examples/images/image_manipulation/js/example.js

89 lines
3.3 KiB
JavaScript
Raw Normal View History

2012-04-14 09:46:46 +00:00
/*
Load the image module.
*/
Ox.load('Image', function() {
/*
2012-06-25 09:28:55 +00:00
Load a sample image (which has its own entry in
2012-04-14 09:46:46 +00:00
<a href="see http://en.wikipedia.org/wiki/Lenna">Wikipedia</a>).
*/
Ox.Image('png/lenna256.png', function(image) {
/*
2012-06-27 10:57:49 +00:00
Create some DOM elements. Ox.$ works like jQuery.
2012-04-14 09:46:46 +00:00
*/
var $body = Ox.$('body'),
2012-06-16 11:55:09 +00:00
$select = Ox.$('<select>').on({change: change}).appendTo($body),
2012-04-14 09:46:46 +00:00
$image = Ox.$('<img>').attr({src: image.src()}).appendTo($body);
[
'Method...', 'src("png/lenna256.png")',
'blur(2)', 'blur(4)',
'channel("r")', 'channel("g")', 'channel("b")',
'channel("h")', 'channel("s")', 'channel("l")',
'contour()',
'depth(1)', 'depth(2)', 'depth(4)',
'drawCircle([128, 128], 64, {"fill": "rgba(0, 0, 0, 0.5)"})',
'drawLine([[64, 64], [192, 192]], {"color": "green", "width": 2})',
'drawPath([[64, 64], [192, 64], [64, 192]], {"close": true})',
'drawRectangle([64, 64], [128, 128], {"fill": "red", "width": 0})',
'drawText("?", [16, 240], {"color": "blue", "font": "64px Arial"})',
'edges()', 'emboss()',
'encode("secret")', 'decode()',
'encode("secret", false)', 'decode(false)',
'encode("secret", 1)', 'decode(1)',
'encode("secret", false, 15)', 'decode(false, 15)',
'encode("secret", 127)', 'decode(127)',
'filter([0, 1, 0, 1, -2, 1, 0, 1, 0])',
'hue(-60)', 'hue(60)',
'invert()',
'lightness(-0.5)', 'lightness(0.5)',
'map(function(v, xy) { return Ox.sum(xy) % 2 ? [0, 0, 0] : v; })',
'mosaic(4)', 'motionBlur()',
'photocopy()', 'pixel([16, 16], [255, 255, 255])', 'posterize()',
'saturation(-0.5)', 'saturation(0.5)',
'sharpen()', 'solarize()'
].forEach(function(method) {
Ox.$('<option>').html(method).appendTo($select);
});
function change() {
var value = $select.val(),
match = value.match(/^(\w+)\((.*?)\)$/),
fn = match[1], args;
/*
2012-06-25 09:28:55 +00:00
The `map` method takes a function as its argument, which we can't
`JSON.parse`, but have to `eval`.
2012-04-14 09:46:46 +00:00
*/
try {
args = JSON.parse('[' + match[2] + ']');
} catch(e) {
args = [eval('f = ' + match[2])];
}
/*
2012-06-25 09:28:55 +00:00
The `src` and `encode` methods are asynchronous and take a callback
function.
2012-04-14 09:46:46 +00:00
*/
if (fn == 'src' || fn == 'encode') {
2012-05-24 10:20:24 +00:00
image[fn].apply(null, args.concat(function(image) {
2012-04-14 09:46:46 +00:00
$image.attr({src: image.src()});
}));
/*
2012-06-25 09:28:55 +00:00
The `decode` method is asynchronous too, and its callback function
gets passed a string.
2012-04-14 09:46:46 +00:00
*/
} else if (fn == 'decode') {
2012-06-25 09:28:55 +00:00
image[fn].apply(null, args.concat(alert));
2012-04-14 09:46:46 +00:00
/*
All other methods simply return the image.
*/
} else {
$image.attr({src: image[fn].apply(null, args).src()});
}
$select.val('Method...');
}
});
2012-06-16 11:55:09 +00:00
});