如何更改 Canvas 上的 svg 颜色 - Fabricjs
How to change svg color on Canvas - Fabricjs
我正在使用 Fabricjs 创建一个应用程序。
我必须向 canvas 添加一个 SVG 文件,并在每次 Minicolors 输入更改时更改颜色。
我首先让浏览器将 SVG 图像显示为 SVG 代码,如下所示:
$('img[src$=".svg"]').each(function(){
var $img = $(this),
imgURL = $img.attr('src'),
attributes = $img.prop('attributes');
$.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// Remove any invalid XML tags as per http://validator.w3.org
$svg = $svg.removeAttr('xmlns:a');
// Make sure that every attribute was copied
$.each(attributes, function() {
$svg.attr(this.name, this.value);
});
// Replace image with new SVG
$img.replaceWith($svg);
}, 'xml');
});
然后我将 SVG 图像从 DOM 加载到 canvas,当它们被点击时,像这样:
$('#images').on('click', 'svg', function() {
var serializer = new XMLSerializer(),
svgStr = serializer.serializeToString(this);
fabric.loadSVGFromString(svgStr,function(objects, options) {
options.id = this.id;
var obj = fabric.util.groupSVGElements(objects, options);
canvas.add(obj);
obj.scaleToHeight(127) // Scales it down to some small size
.scaleToWidth(90)
.center() // Centers it (no s**t, Sherlock)
.setCoords();
canvas.setActiveObject(obj).renderAll();
});
});
现在我的下一个目标是如何更改所选 svg 文件的路径颜色?
我的主要猜测是遵循以下步骤:
- 保存所选 SVG 的左侧和顶部位置
- 更改 DOM
中 SVG 图像的颜色
- 删除选定的 SVG 对象
- 在保存的左侧和顶部位置使用新颜色将其替换为新的 SVG
但我想:"So I have to do all this every time a Minicolors input changes? Won't that be a performance issue later?"
还有比这更好的方法吗?这里有一个 JSFiddle 可以帮助您入门。谢谢。
添加到 canvas 后,SVG 对象包含一个名为 "paths" 的 属性,其中包含构建图像的所有路径。所以我们这样做:
activeObject.paths.forEach(function(path) {path.fill = color});
但我想知道这是否会成为大型 SVG 文件的性能问题(希望我不会说到那一点)。这是一个有效的 JSFiddle.
的回答不适用于最新版本的 fabricjs。
就我而言,我正在使用 fabricjs 3.5
。
在最新版本的fabricjs中,paths
属性已被移除。 paths
数据已添加到 _objects
属性.
就是这样,我让它为我工作。
var obj = this.canvas.itemObj;
var color = '#ff00ff';
if (obj && obj._objects) {
for (var i = 0; i < obj._objects.length; i++) {
obj._objects[i].set({
fill: color
});
}
}
我正在使用 Fabricjs 创建一个应用程序。
我必须向 canvas 添加一个 SVG 文件,并在每次 Minicolors 输入更改时更改颜色。
我首先让浏览器将 SVG 图像显示为 SVG 代码,如下所示:
$('img[src$=".svg"]').each(function(){
var $img = $(this),
imgURL = $img.attr('src'),
attributes = $img.prop('attributes');
$.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = $(data).find('svg');
// Remove any invalid XML tags as per http://validator.w3.org
$svg = $svg.removeAttr('xmlns:a');
// Make sure that every attribute was copied
$.each(attributes, function() {
$svg.attr(this.name, this.value);
});
// Replace image with new SVG
$img.replaceWith($svg);
}, 'xml');
});
然后我将 SVG 图像从 DOM 加载到 canvas,当它们被点击时,像这样:
$('#images').on('click', 'svg', function() {
var serializer = new XMLSerializer(),
svgStr = serializer.serializeToString(this);
fabric.loadSVGFromString(svgStr,function(objects, options) {
options.id = this.id;
var obj = fabric.util.groupSVGElements(objects, options);
canvas.add(obj);
obj.scaleToHeight(127) // Scales it down to some small size
.scaleToWidth(90)
.center() // Centers it (no s**t, Sherlock)
.setCoords();
canvas.setActiveObject(obj).renderAll();
});
});
现在我的下一个目标是如何更改所选 svg 文件的路径颜色? 我的主要猜测是遵循以下步骤:
- 保存所选 SVG 的左侧和顶部位置
- 更改 DOM 中 SVG 图像的颜色
- 删除选定的 SVG 对象
- 在保存的左侧和顶部位置使用新颜色将其替换为新的 SVG
但我想:"So I have to do all this every time a Minicolors input changes? Won't that be a performance issue later?"
还有比这更好的方法吗?这里有一个 JSFiddle 可以帮助您入门。谢谢。
添加到 canvas 后,SVG 对象包含一个名为 "paths" 的 属性,其中包含构建图像的所有路径。所以我们这样做:
activeObject.paths.forEach(function(path) {path.fill = color});
但我想知道这是否会成为大型 SVG 文件的性能问题(希望我不会说到那一点)。这是一个有效的 JSFiddle.
就我而言,我正在使用 fabricjs 3.5
。
在最新版本的fabricjs中,paths
属性已被移除。 paths
数据已添加到 _objects
属性.
就是这样,我让它为我工作。
var obj = this.canvas.itemObj;
var color = '#ff00ff';
if (obj && obj._objects) {
for (var i = 0; i < obj._objects.length; i++) {
obj._objects[i].set({
fill: color
});
}
}