简体   繁体   English

Fabric.js将过滤器应用于Image.fromURL

[英]Fabric.js apply filter to Image.fromURL

I have a fabricjs canvas [v2.4] and i'm trying to add an image via url and then apply a filter (grayscale for example). 我有一个fabricjs canvas [v2.4],我试图通过url添加图像,然后应用过滤器(例如灰度)。

I've managed to add the image via url but i cant quite get the filter part right. 我设法通过url添加图像,但是我不能完全正确地使用过滤器。 I've followed some examples online but i always get the following error: 我在网上关注了一些示例,但始终会收到以下错误:

Uncaught TypeError: (intermediate value)(intermediate value).filter is not a function

Here's a short snipper of the code that involves the adding + filter while you can see the full fiddle attached below as well. 这是一段简短的代码片段,其中涉及添加+过滤器,同时您还可以在下面看到完整的小提琴。 The two lines of code that does that filtering is uncommented for now to not cause the error. 暂时没有注释执行过滤的两行代码,不会引起该错误。

var img_url = "https://www.fariskassim.com/stage/rebel9/seongbukgu/mobile_browser_cam/v2/img/test.png"

// add image to fabriccanvas
function addImg_d() {

  fabric.Image.fromURL(img_url, function(img) {
    // uncomment the 2 lines below and you'll get an error
    //img.filters.push(new fabric.Image.filters.Grayscale());
    //img.applyFilters(d_canvas.renderAll.bind(d_canvas));
    img.scale(1);
    d_canvas.add(img);
  });
};

I've also tried different images / base64 data but i still get the exact same error. 我也尝试过不同的图像/ base64数据,但是我仍然得到完全相同的错误。 Any ideas ? 有任何想法吗 ? Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

 // init fabric canvas var d_canvas = new fabric.Canvas('d_canvas', { isDrawingMode: false, selection: false }); d_canvas.enableGLFiltering = false; // resize canvas on resize window.addEventListener('resize', resizeCanvas_d, false); function resizeCanvas_d() { d_canvas.setDimensions({ width: 300, height: 200 }); } // resize on init resizeCanvas_d(); // click to add image var captureButton = document.getElementById('capture'); captureButton.addEventListener('click', () => { setTimeout(function() { addImg_d() }, 100); }); var img_url = "https://www.fariskassim.com/stage/rebel9/seongbukgu/mobile_browser_cam/v2/img/test.png" // add image to fabriccanvas function addImg_d() { fabric.Image.fromURL(img_url, function(img) { // uncomment the 2 lines below and you'll get an error //img.filters.push(new fabric.Image.filters.Grayscale()); //img.applyFilters(d_canvas.renderAll.bind(d_canvas)); img.scale(1); d_canvas.add(img); }); }; 
 html, body { margin: 0; overflow: hidden; } #capture { position: fixed; z-index: 100; bottom: 0; left: 50%; transform: translate(-50%); font-size: 20px; padding: 10px; background-color: red; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.0/fabric.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <canvas id="d_canvas" style="border:1px solid #ccc"></canvas> <button id="capture">Add</button> 

Call img.applyFilters() no need to pass argument as you are setting filter. 调用img.applyFilters()无需传递参数,因为您正在设置过滤器。

DEMO DEMO

 // init fabric canvas var d_canvas = new fabric.Canvas('d_canvas', { isDrawingMode: false, selection: false }); d_canvas.enableGLFiltering = false; // resize canvas on resize window.addEventListener('resize', resizeCanvas_d, false); function resizeCanvas_d() { d_canvas.setDimensions({ width: 800, height: 600 }); } // resize on init resizeCanvas_d(); // click to add image var captureButton = document.getElementById('capture'); captureButton.addEventListener('click', () => { setTimeout(function() { addImg_d() }, 100); }); var img_url = "https://cdn.shopify.com/s/files/1/1061/1924/files/Eye_Roll_Emoji_large.png?v=1541768914" // add image to fabriccanvas function addImg_d() { fabric.Image.fromURL(img_url, function(img) { // uncomment the 2 lines below and you'll get an error img.filters.push(new fabric.Image.filters.Grayscale()); img.applyFilters(); img.scale(1); d_canvas.add(img); },{crossOrigin:'anonymous'}); }; 
 html, body { margin: 0; overflow: hidden; } #capture { position: fixed; z-index: 100; bottom: 0; left: 50%; transform: translate(-50%); font-size: 20px; padding: 10px; background-color: red; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.0/fabric.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <canvas id="d_canvas" style="border:1px solid #ccc"></canvas> <button id="capture">Add</button> 

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

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