gpt4 book ai didi

javascript - 我想应用过滤器 : blur to any shape

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

点击图片点击路径。在击中三个或更多传球后单击第一个点以关闭传球。当路径关闭时,我想选择路径的内部并实现模糊范围的功能。目前,关闭路径时不会发生任何事情。

最佳答案

我认为你弄错了 Paper.js渲染引擎。
它在 <canvas> 的上下文中绘制其项目元素,因此您将能够从开发人员工具访问的只是此 Canvas 及其图像数据。
您将无法定位您的路径并使用选择器对其进行操作,这似乎是您正在尝试做的事情。

无论如何,不​​幸的是,Paper.js目前不支持过滤器。
因此,一种选择可能是利用 Canvas 上下文 filter属性(实验性)或自己实现模糊算法。
然后,同时保持 Paper.js绘图实用程序,您可以管理多个 Canvas 并进行智能合成以产生您正在寻找的效果。

这是一个fiddle演示一个可能的实现。
请注意,为了演示,我简化了您的用例,但您应该能够很容易地使其适应您的情况。
在这个例子中,我使用了 3 个不同的 Canvas :
- 最下面的是绘制原图
- 中间的是画模糊部分
- 最上面的用于绘制我们将用于合成的形状,并将在最后隐藏

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Debug Paper.js</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.12.2/paper-core.min.js"></script>
<style>
html,
body {
margin : 0;
overflow : hidden;
height : 100%;
}

/* We position canvases on top of each other. */
canvas {
position : absolute;
top : 0;
left : 0;
width : 100vw;
height : 100vh;
}
</style>
</head>
<body>
<canvas id="bottom-canvas"></canvas>
<canvas id="middle-canvas"></canvas>
<canvas id="top-canvas"></canvas>
<script>
// Get canvases references.
const bottomCanvas = document.getElementById('bottom-canvas');
const middleCanvas = document.getElementById('middle-canvas');
const topCanvas = document.getElementById('top-canvas');

// Initialise 2 PaperScopes.
const bottomScope = new paper.PaperScope();
bottomScope.setup(bottomCanvas);
const topScope = new paper.PaperScope();
topScope.setup(topCanvas);

// For middle canvas, we need to adjust the size manually as Paper.js doesn't handle it.
middleCanvas.width = middleCanvas.offsetWidth;
middleCanvas.height = middleCanvas.offsetHeight;

// Draw the image on the bottom canvas.
new paper.Raster({
source: 'https://i.imgur.com/6N0Zwag.jpg',
crossOrigin: 'anonymous',
position: bottomScope.view.center,
parent: bottomScope.project.activeLayer,
// When image is loaded...
onLoad: function() {
// ...make it fit the whole canvas.
this.fitBounds(bottomScope.view.bounds, true);

// Draw a circle on the top canvas that represents the user drawn shape
// that we want to use for blurring.
new paper.Path.Circle({
center: topScope.view.center,
radius: 200,
fillColor: 'orange',
parent: topScope.project.activeLayer
});

// We manually call the canvas view update to make sure that everything
// is drawn before we play with image data.
bottomScope.view.update();
topScope.view.update();

// Get middle canvas context to be able to draw on it.
const middleCanvasContext = middleCanvas.getContext('2d');
// Draw the bottom canvas image on the middle canvas with the blur filter applied.
middleCanvasContext.filter = 'blur(15px)';
middleCanvasContext.drawImage(bottomCanvas, 0, 0);

// In order to see the clear part of the bottom canvas image,
// we need to remove from middle canvas what is not on top canvas.
// For that, we use "destination-in" composite operation.
middleCanvasContext.filter = 'none';
middleCanvasContext.globalCompositeOperation = 'destination-in';
middleCanvasContext.drawImage(topCanvas, 0, 0);

// Now, we need to hide the top canvas, to see the middle one below it.
topCanvas.style.display = 'none';
}
});
</script>
</body>
</html>

关于javascript - 我想应用过滤器 : blur to any shape,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56885385/

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