【发布时间】:2020-07-26 14:32:18
【问题描述】:
我通过 fabric.js 设置了一个形状,我正在尝试使用 fill 同时实现 2 个目标:
1) 一个 rgba 填充颜色 rgba(255,0,0,0.5)
与
2) 带有透明 SVG 图像的 fabric.Pattern。
我可以使用 SVG 图像分别制作 fill 颜色和 fill fabric.Pattern,但我不知道如何将它们一起制作
我正在使用的代码:
var canvas = this.__canvas = new fabric.Canvas('c');
//To set the pattern with an SVG image
function loadPattern(url) {
fabric.util.loadImage(url, function(img) {
shape.set('fill', new fabric.Pattern({
source: img,
repeat: 'repeat'
}));
canvas.renderAll();
});
}
//To set just a color
function setColor(color) {
shape.set('fill', color);
canvas.renderAll();
}
//To set the backgroundColor
function setbackgroundColor(color) {
shape.set('backgroundColor', color);
canvas.renderAll();
}
var shape = new fabric.Polyline([
{x: 78, y: 138},
{x: 146, y: 96},
{x: 170, y: 117},
{x: 275, y: 127},
{x: 275, y: 216},
{x: 78, y: 138}
], {
fill: 'rgba(255,0,0,0.5)',
stroke: 'red',
left: 100,
top: 100
});
canvas.add(shape);
我已经尝试使用 fill 和模式加上 ```shape.backgroundColor = 'rgba(255,0,0,0.5)';
// initialize fabric canvas and assign to global windows object for debug
var canvas = window._canvas = new fabric.Canvas('c');
//To set the pattern with an SVG image
function loadPattern(url) {
fabric.util.loadImage(url, function(img) {
shape.set('fill', new fabric.Pattern({
source: img,
repeat: 'repeat'
}));
canvas.renderAll();
});
}
//To set just a color
function setColor(color) {
shape.set('fill', color);
canvas.renderAll();
}
//
function setbackgroundColor(color) {
shape.set('backgroundColor', color);
canvas.renderAll();
}
var shape = new fabric.Polyline([
{x: 78, y: 138},
{x: 146, y: 96},
{x: 170, y: 117},
{x: 275, y: 127},
{x: 275, y: 216},
{x: 78, y: 138}
], {
fill: 'rgba(255,0,0,0.5)',
stroke: 'red',
left: 100,
top: 100
});
canvas.add(shape);
canvas {
border: 1px solid #999;
}
button {
margin-top: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.3/fabric.min.js"></script>
<button onclick="loadPattern('https://www.heropatterns.com/svg/anchors-away.svg');">Set pattern</button>
<button onclick="setColor('rgba(255,0,0,0.5)');">Set color</button>
<button onclick="setbackgroundColor('rgba(255,0,0,0.5)');">Set backgroundColor</button>
<button onclick="setbackgroundColor('transparent');">Set backgroundColor to transparent</button>
<canvas id="c" width="400" height="400"></canvas>
这是我的 jsFiddle: http://jsfiddle.net/shehan11/1vgps2dw/5/
感谢您的帮助!
【问题讨论】:
标签: javascript svg html5-canvas fabricjs jsfiddle