From 581b87baf9e78ca3db22ea904ba0372783342081 Mon Sep 17 00:00:00 2001 From: rolux Date: Sat, 14 Apr 2012 11:46:23 +0200 Subject: [PATCH] small updates to Ox.Image --- source/Ox.Image/Ox.Image.js | 69 +++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/source/Ox.Image/Ox.Image.js b/source/Ox.Image/Ox.Image.js index 69e78360..2ac445f1 100644 --- a/source/Ox.Image/Ox.Image.js +++ b/source/Ox.Image/Ox.Image.js @@ -49,13 +49,7 @@ Ox.load.Image = function(options, callback) { return capacity; } - function getIndex() { - var xy; - if (arguments.length == 1) { - xy = arguments[0]; - } else { - xy = [arguments[0], arguments[1]]; - } + function getIndex(xy) { return ( Ox.mod(xy[0], self.width) + Ox.mod(xy[1] * self.width, self.width * self.height) @@ -90,6 +84,15 @@ Ox.load.Image = function(options, callback) { self.callback(that); } + function parseDrawOptions(options) { + options = options || {}; + that.context.strokeStyle = options.width === 0 + ? 'rgba(0, 0, 0, 0)' : options.color || 'rgb(0, 0, 0)'; + that.context.fillStyle = options.fill || 'rgba(0, 0, 0, 0)'; + that.context.lineWidthWidth = options.width !== void 0 + ? options.width : 1; + } + function setSL(sl, d) { var c = sl == 's' ? 1 : 2; return that.map(function(rgba) { @@ -191,10 +194,7 @@ Ox.load.Image = function(options, callback) { width Line width in px @*/ that.drawCircle = function(point, radius, options) { - options = options || {}; - that.context.strokeStyle = options.color || 'rgb(0, 0, 0)'; - that.context.fillStyle = options.fill || 'rgba(0, 0, 0, 0)'; - that.context.lineWidth = options.width || 1; + parseDrawOptions(options); that.context.beginPath(); that.context.arc(point[0], point[1], radius, 0, 2 * Math.PI); that.context.fill(); @@ -211,9 +211,7 @@ Ox.load.Image = function(options, callback) { width Line width in px @*/ that.drawLine = function(points, options, isPath) { - options = options || {}; - that.context.strokeStyle = options.color || 'rgb(0, 0, 0)'; - that.context.lineWidth = options.width || 1; + parseDrawOptions(options); !isPath && that.context.beginPath(); !isPath && that.context.moveTo(points[0][0], points[0][1]); that.context.lineTo(points[1][0], points[1][1]); @@ -232,8 +230,7 @@ Ox.load.Image = function(options, callback) { @*/ that.drawPath = function(points, options) { var n = points.length; - options = options || {}; - that.context.fillStyle = options.fill || 'rgba(0, 0, 0, 0)'; + parseDrawOptions(options); that.context.beginPath(); that.context.moveTo(points[0][0], points[0][1]); Ox.loop(options.close ? n : n - 1, function(i) { @@ -255,10 +252,7 @@ Ox.load.Image = function(options, callback) { width Line width in px @*/ that.drawRectangle = function(point, size, options) { - options = options || {}; - that.context.strokeStyle = options.color || 'rgb(0, 0, 0)'; - that.context.fillStyle = options.fill || 'rgba(0, 0, 0, 0)'; - that.context.lineWidth = options.width || 1; + parseDrawOptions(options); that.context.fillRect(point[0], point[1], size[0], size[1]); that.context.strokeRect(point[0], point[1], size[0], size[1]); return that; @@ -310,9 +304,9 @@ Ox.load.Image = function(options, callback) { @*/ that.emboss = function(val) { return that.filter([ - -1, -1, +0, - -1, +0, +1, - +0, +1, +1 + -1, -1, 0, + -1, 0, +1, + 0, +1, +1 ], 128).saturation(-1); }; @@ -500,7 +494,7 @@ Ox.load.Image = function(options, callback) { }); Ox.loop(-d, d + 1, function(x) { Ox.loop(-d, d + 1, function(y) { - var pixelIndex = getIndex(xy[0] + x, xy[1] + y); + var pixelIndex = getIndex([xy[0] + x, xy[1] + y]); Ox.loop(3, function(c) { data[i + c] += self.data[pixelIndex + c] * filter[filterIndex]; }); @@ -631,17 +625,23 @@ Ox.load.Image = function(options, callback) { if (xy[0] % size == 0 && xy[1] % size == 0) { Ox.loop(size, function(x) { Ox.loop(size, function(y) { - var hsl, rgb; - if ((x == 0 || y == 0) && !(x == size - 1 || y == size - 1)) { - that.pixel(xy[0] + x, xy[1] + y, rgba.map(function(c, i) { + var hsl, rgb, xy_ = [xy[0] + x, xy[1] + y]; + if ( + (x == 0 || y == 0) + && !(x == size - 1 || y == size - 1) + ) { + that.pixel(xy_, rgba.map(function(c, i) { return i < 3 ? Math.min(c + 16, 255) : c; })); - } else if ((x == size - 1 || y == size - 1) && !(x == 0 || y == 0)) { - that.pixel(xy[0] + x, xy[1] + y, rgba.map(function(c, i) { + } else if ( + (x == size - 1 || y == size - 1) + && !(x == 0 || y == 0) + ) { + that.pixel(xy_, rgba.map(function(c, i) { return i < 3 ? Math.max(c - 16, 0) : c; })); } else { - that.pixel(xy[0] + x, xy[1] + y, rgba); + that.pixel(xy_, rgba); } }); }); @@ -681,17 +681,20 @@ Ox.load.Image = function(options, callback) { y Y coordinate val <[n]> RGBA values @*/ - that.pixel = function(x, y, val) { - var i = getIndex(x, y); + that.pixel = function(xy, val) { + var i = getIndex(xy), ret; if (!val) { - return Ox.range(4).map(function(c) { + ret = Ox.range(4).map(function(c) { return self.data[i + c]; }); } else { val.forEach(function(v, c) { self.data[i + c] = v; }); + that.context.putImageData(self.imageData, 0, 0); + ret = that; } + return ret; } /*@