gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:47:47 30 4
gpt4 key购买 nike

我有一个 fabricjs canvas [v2.4],我正在尝试通过 url 添加图像,然后应用过滤器(例如灰度)。

我已经设法通过 url 添加图像,但我不能完全正确地设置过滤器部分。我在网上遵循了一些示例,但我总是收到以下错误:

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

这是涉及添加 + 过滤器的代码的简短片段,同时您还可以在下面看到完整的 fiddle 。执行该过滤的两行代码暂时取消注释,以免导致错误。

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);
});
};

我也尝试过不同的图像/base64 数据,但我仍然得到完全相同的错误。有任何想法吗 ?任何帮助表示赞赏。谢谢

// 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>

最佳答案

调用 img.applyFilters() 时无需传递参数,因为您正在设置过滤器。

演示

// 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>

关于javascript - Fabric.js 将过滤器应用于 Image.fromURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52645863/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com