gpt4 book ai didi

javascript - 将样式添加到 `clipPath` Fabric.js 对象

转载 作者:行者123 更新时间:2023-12-04 10:15:25 26 4
gpt4 key购买 nike

我需要一个特定形状的 Canvas 。感谢 this post,我能够这样做使用 clipPath .问题是我根本无法设计它。

这是一个带有圆形 Canvas 的示例。我尝试将相同的样式应用于容器内的对象和容器本身,但正如您所看到的,只有容器内的对象具有样式。如何将笔触或阴影应用到此圆形容器 ( clipPath )?

const canvas = new fabric.Canvas("canvas", {backgroundColor: "#d3d3d3"})

const container = new fabric.Circle({
radius: 150,
left: 50,
top: 50,
stroke: "rgba(0, 0, 0, 0.5)",
strokeWidth: 1,
})

const obj = new fabric.Rect({
width: 100,
height: 100,
left: 150,
top: 150,
fill: "blue",
stroke: "rgba(0, 0, 0, 0.5)",
strokeWidth: 1,
})

const shadow = new fabric.Shadow({
color: "rgba(0, 0, 0, 0.5)",
blur: 10,
})

container.set("shadow", shadow)
obj.set("shadow", shadow)

canvas.clipPath = container
canvas.add(obj)

canvas.requestRenderAll()
#canvas {
background: #e8e8e8;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.0.0-beta.5/fabric.min.js"></script>

<canvas id="canvas" width="400" height="400"></canvas>


[编辑] 我找到了一家这样做的公司: https://www.plaqueomatic.fr/plaqueomatic/plastique .他们使用织物 v3.6.0 .我怎样才能达到同样的目标?

enter image description here

最佳答案

剪裁 Canvas 本身时,最好使用 CSS 应用像阴影这样的样式。我还启用了 controlsAboveOverlay属性使调整大小控件在剪辑路径之外可见(如果不需要,请随意禁用)。

const canvas = new fabric.Canvas("canvas", {
backgroundColor: "#d3d3d3",
controlsAboveOverlay: true
})

const container = new fabric.Circle({
radius: 150,
left: 50,
top: 50,
stroke: "rgba(0, 0, 0, 0.5)",
strokeWidth: 1,
})

const obj = new fabric.Rect({
width: 100,
height: 100,
left: 150,
top: 150,
fill: "blue",
stroke: "rgba(0, 0, 0, 0.5)",
strokeWidth: 1,
})

const shadow = new fabric.Shadow({
color: "rgba(0, 0, 0, 0.5)",
blur: 10,
})

obj.set("shadow", shadow)

canvas.clipPath = container
canvas.add(obj)

canvas.requestRenderAll()
#canvas {
-webkit-filter: drop-shadow(0px 3px 3px rgba(0, 0, 0, 0.5));
filter: drop-shadow(0px 3px 3px rgba(0, 0, 0, 0.5));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.0.0-beta.5/fabric.min.js"></script>

<canvas id="canvas" width="400" height="400"></canvas>

关于javascript - 将样式添加到 `clipPath` Fabric.js 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61084654/

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