简体   繁体   English

Fabric.js中的PNG图像颜色不会更改

[英]PNG image color doesn't change in Fabric.js

I am using below code to change color of a PNG image on canvas dynamically, 我使用下面的代码动态地更改画布上PNG图像的颜色,

var selectedObject = canvas.getActiveObject();

if ("text" == selectedObject.type) {
   selectedObject.setFill("#FF0000");
   canvas.renderAll();
}
else {    
  selectedObject.filters.push(
    new fabric.Image.filters.Tint({color:"#FF0000", opacity:0.6}));

  selectedObject.applyFilters(canvas.renderAll.bind(canvas));
}

Where canvas = new fabric.Canvas('c'); canvas = new fabric.Canvas('c');

*Static colors are added for testing purpose which will be replaced by colorpicker. *添加静态颜色用于测试目的,将由颜色选择器替换。

For text it is working fine but for image it is not working. 对于文本它工作正常,但对于图像,它不起作用。

You can't apply image filters on crossOrigin images. 您无法在crossOrigin图像上应用图像过滤器。 If you look at developer console you see the following security error: "Uncaught SecurityError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': the canvas has been tainted by cross-origin data." 如果您查看开发人员控制台,您会看到以下安全错误:“Uncaught SecurityError:无法在'CanvasRenderingContext2D'上执行'getImageData':画布已被跨源数据污染。”

Please use only images from same origin or use latest Fabric.js version (dev version) and set crossOrigin = "Anonymous" . 请仅使用来自同一来源的图像或使用最新的Fabric.js版本(开发版)并设置crossOrigin = "Anonymous" In this case the image have to be served with the header "Access-Control-Allow-Origin: *". 在这种情况下,图像必须与标题“Access-Control-Allow-Origin:*”一起提供。

You use the new API of fabric.Image.filters.Tint - this API is only available in Fabric.js versions >= 1.3.2. 您可以使用新的API fabric.Image.filters.Tint -这个API仅适用于Fabric.js版本> = 1.3.2可用。

I've updated the jsfiddle to use data-URL images (no problem with CORS) and update Fabric.js to 1.3.2: http://jsfiddle.net/Kienz/wDfhf/ 我已经更新了jsfiddle以使用数据URL图像(CORS没有问题)并将Fabric.js更新为1.3.2: http//jsfiddle.net/Kienz/wDfhf/

You can find the latest dev version (by your own risk:)) at https://github.com/kangax/fabric.js/tree/master/dist 您可以在https://github.com/kangax/fabric.js/tree/master/dist找到最新的开发版本(风险自行:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM