app/assets/javascript/pageflow/linkmap_page/widgets/linkmap/image_data.js in pageflow-linkmap-page-1.5.0 vs app/assets/javascript/pageflow/linkmap_page/widgets/linkmap/image_data.js in pageflow-linkmap-page-2.0.0
- old
+ new
@@ -1,74 +1,44 @@
pageflow.linkmapPage.ImageData = (function() {
- function ImageData(width, height) {
+ function ImageData(width, height, image) {
var canvas = document.createElement('canvas');
this.width = width;
this.height = height;
canvas.width = width;
canvas.height = height;
- this.loadImage = function(image) {
- image.draw(canvas, {disableImageSmoothing: true});
- };
+ image.draw(canvas.getContext('2d'), {
+ width: width,
+ disableImageSmoothing: true
+ });
this.draw = function(context, sx, sy, sw, sh, dx, dy, dw, dh) {
context.drawImage(canvas, sx, sy, sw, sh, dx, dy, dw, dh);
};
- this.copyColorFrom = function(imageData, color, destinationX, destinationY) {
- var data = imageData.data;
- var destinationContext = canvas.getContext('2d');
-
- for (var i = 0; i < data.length; i += 4) {
- if (data[i] != color[0] ||
- data[i + 1] != color[1] ||
- data[i + 2] != color[2]) {
-
- data[i + 3] = 0;
- }
- else {
- data[i + 3] = 255;
- }
- }
-
- destinationContext.putImageData(imageData, destinationX, destinationY);
- };
-
- this.getBoundingBox = function(box) {
- return this.get(box.left, box.top, box.width, box.height);
- };
-
- this.get = function(left, top, width, height) {
+ this.nonTransparentAt = function(x, y) {
var context = canvas.getContext('2d');
- return context.getImageData(left, top, width, height);
- };
+ var pixel = context.getImageData(x, y, 1, 1).data;
- this.toDataURL = function() {
- var result = canvas.toDataURL();
-
- if (!result.match(/^data:image/)) {
- throw new invalidImageDataUrl();
- }
-
- return result;
+ return pixel[3] > 0;
};
}
- function invalidImageDataUrl() {
- var error = new Error('Invalid data url from canvas.');
- error.i18nKey = 'pageflow.linkmap_page.errors.invalid_image_data';
+ ImageData.empty = {
+ hasColorAt: function() {
+ return false;
+ },
- return error;
- }
+ colorAt: function() {
+ return null;
+ }
+ };
ImageData.load = function(url) {
return pageflow.linkmapPage.RemoteImage.load(url).then(function(image) {
- var imageData = new ImageData(image.width(), image.height());
- imageData.loadImage(image);
-
- return imageData;
+ return new ImageData(image.width(), image.height(), image);
});
};
return ImageData;
}());