将修改后的 SVG 绘制到画布上

2023-06-20前端开发问题
3

本文介绍了将修改后的 SVG 绘制到画布上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想加载一个 SVG 图像,对其 .contentDocument 进行一些操作,然后将其绘制到画布上.

I want to load an SVG image, do some manipulations to its .contentDocument, and then draw it to a canvas.

将 SVG 绘制到画布上的一个很好的例子是:http://www.phrogz.net/tmp/canvas_from_svg.html

A good example for drawing an SVG to a canvas is here: http://www.phrogz.net/tmp/canvas_from_svg.html

但在本例中,svg 被创建为 new Image('url.svg') 对象.当您以这种方式创建 SVG 时,不幸的是,它似乎没有要操作的 contentDocument.当您将其创建为 <object> 元素时,它似乎只有一个.

But in this example the svg was created as a new Image('url.svg') object. When you create an SVG that way, it unfortunately doesn't seem to have a contentDocument to manipulate. It only seems to have one when you create it as an <object> element.

但是当我将 SVG 创建为对象时,获取 SVG 的 DOM 节点并将其传递给 context.drawImage(svgNode, x, y),它会抛出错误 "Value could不能转换为以下任何一种:HTMLImageElement、HTMLCanvasElement、HTMLVideoElement."(在 Firefox 上).

But when I create the SVG as an object, get the SVG's DOM node and pass it to context.drawImage(svgNode, x, y), it throws the error "Value could not be converted to any of: HTMLImageElement, HTMLCanvasElement, HTMLVideoElement." (on Firefox).

看来我要么必须找到将对象 SVG 转换为 HTMLImageElement 的方法,要么必须找到一种方法来获取作为图像加载的 SVG 的内容文档.有谁知道该怎么做?还是有第三种方法可以做到这一点,我错过了?

It seems like I either have to find a way to convert an object-SVG to a HTMLImageElement or a way to get the content document of an SVG which was loaded as an Image. Does anyone know how to do either? Or is there a third way to do it which I am missing?

推荐答案

我设法做到了.诀窍是:

I managed to do it. The trick was to:

  1. 使用 XMLHttpRequest() 将 SVG 加载为 XML 文档
  2. 操作该 XML 文档
  3. 将 XML 文档转换为字符串
  4. 从该字符串创建一个 ObjectURL
  5. 使用该 ObjectURL 创建一个图像
  6. 将该图像复制到画布

这是我的源代码:

不幸的是,它只适用于 Firefox 和 Chrome.它在 IE9 中失败,因为不支持 XMLSerializer()(它也不支持 XML 文档上的 getElementById,但有一些解决方法),它在 Opera 中失败,因为不支持 createObjectUrl.

Unfortunately it only works in Firefox and Chrome. It fails in IE9 because XMLSerializer() is not supported (it also doesn't support getElementById on XML documents, but there are workarounds for that) and it fails in Opera because createObjectUrl is not supported.

var xhr = new XMLHttpRequest();
xhr.onload = function() {
    // get the XML tree of the SVG
    var svgAsXml = xhr.responseXML;
    // do some modifications to the XML tree
    var element = svgAsXml.getElementById('hat');
    element.style.fill = '#ffff00';
    // convert the XML tree to a string
    var svgAsString = new XMLSerializer().serializeToString(svgAsXml);
    // create a new image with the svg string as an ObjectUrl
    var svgBlob = new Blob([svgAsString], {type: "image/svg+xml;charset=utf-8"});
    var url = window.URL.createObjectURL(svgBlob);
    var img = new Image();
    img.src = url;
    // copy it to the canvas
    img.onload = function() {
        var theCanvas = document.getElementById('theCanvas');
        var context = theCanvas.getContext('2d');
        context.drawImage(img, 0, 0);
        window.URL.revokeObjectURL(svgBlob);
    }
}
xhr.open("GET", "test.svg");
xhr.responseType = "document";
xhr.send();

这篇关于将修改后的 SVG 绘制到画布上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用百度地图API获取地理位置信息
首先,我们需要在百度地图开放平台上申请一个开发者账号,并创建一个应用。在创建应用的过程中,我们会得到一个密钥(ak),这是调用API的凭证。 接下来,我们需要准备一个PHP文件,以便可以在网页中调用。首先,我们需要引入百度地图API的JS文件,代码如下...
2024-11-22 前端开发问题
244

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

正则表达式([A-Za-z])为啥可以匹配字母加数字或特殊符号?
问题描述: 我需要在我的应用程序中验证一个文本字段。它既不能包含数字,也不能包含特殊字符,所以我尝试了这个正则表达式:/[a-zA-Z]/匹配,问题是,当我在字符串的中间或结尾放入一个数字或特殊字符时,这个正则表达式依然可以匹配通过。 解决办法: 你应...
2024-06-06 前端开发问题
165