Javascript Canvg |将SVG转换为画布以作为图像输出
我在SVG中有一个复杂的交互式图形。我想把SVG变成一个隐藏的画布,这样我就可以允许用户以png/pdf格式输出 test111.js创建div#forSVG,然后在其中创建svg#svg(加上圆圈、路径和文本) 有不同的错误,但它们都在canvg.js的第50行附近,我怀疑它们都与s变量未定义有关。编辑1640:canvg.js的相关行告诉我们s是什么:Javascript Canvg |将SVG转换为画布以作为图像输出,javascript,html,canvas,svg,canvg,Javascript,Html,Canvas,Svg,Canvg,我在SVG中有一个复杂的交互式图形。我想把SVG变成一个隐藏的画布,这样我就可以允许用户以png/pdf格式输出 test111.js创建div#forSVG,然后在其中创建svg#svg(加上圆圈、路径和文本) 有不同的错误,但它们都在canvg.js的第50行附近,我怀疑它们都与s变量未定义有关。编辑1640:canvg.js的相关行告诉我们s是什么: this.canvg = function (target, s, opts) { 因此,当我调用canvg(canvas,svg)时,我
this.canvg = function (target, s, opts) {
因此,当我调用canvg(canvas,svg)时,我认为s是我输入的svg变量(选择可选)。Console.logging(svg类型)返回对象
canvg.js的前60行:
/*
* canvg.js - Javascript SVG parser and renderer on Canvas
* MIT Licensed
* Gabe Lerner (gabelerner@gmail.com)
* http://code.google.com/p/canvg/
*
* Requires: rgbcolor.js - http://www.phpied.com/rgb-color-parser-in-javascript/
*/
(function(){
// canvg(target, s)
// empty parameters: replace all 'svg' elements on page with 'canvas' elements
// target: canvas element or the id of a canvas element
// s: svg string, url to svg file, or xml document
// opts: optional hash of options
// ignoreMouse: true => ignore mouse events
// ignoreAnimation: true => ignore animations
// ignoreDimensions: true => does not try to resize canvas
// ignoreClear: true => does not clear canvas
// offsetX: int => draws at a x offset
// offsetY: int => draws at a y offset
// scaleWidth: int => scales horizontally to width
// scaleHeight: int => scales vertically to height
// renderCallback: function => will call the function after the first render is completed
// forceRedraw: function => will call the function on every frame, if it returns true, will redraw
this.canvg = function (target, s, opts) {
// no parameters
if (target == null && s == null && opts == null) {
var svgTags = document.querySelectorAll('svg');
for (var i=0; i<svgTags.length; i++) {
var svgTag = svgTags[i];
var c = document.createElement('canvas');
c.width = svgTag.clientWidth;
c.height = svgTag.clientHeight;
svgTag.parentNode.insertBefore(c, svgTag);
svgTag.parentNode.removeChild(svgTag);
var div = document.createElement('div');
div.appendChild(svgTag);
canvg(c, div.innerHTML);
}
return;
}
if (typeof target == 'string') {
target = document.getElementById(target);
}
// store class on canvas
if (target.svg != null) target.svg.stop();
var svg = build(opts || {});
// on i.e. 8 for flash canvas, we can't assign the property so check for it
if (!(target.childNodes.length == 1 && target.childNodes[0].nodeName == 'OBJECT')) target.svg = svg;
var ctx = target.getContext('2d');
if (typeof(s.documentElement) != 'undefined') {
// load from xml doc
svg.loadXmlDoc(ctx, s);
}
else if (s.substr(0,1) == '<') {
// load from xml string
svg.loadXml(ctx, s);
}
else {
// load from url
svg.load(ctx, s);
}
}
/*
*canvg.js-画布上的Javascript SVG解析器和渲染器
*麻省理工学院许可
*盖布·勒纳(gabelerner@gmail.com)
* http://code.google.com/p/canvg/
*
*需要:rgbcolor.js-http://www.phpied.com/rgb-color-parser-in-javascript/
*/
(功能(){
//canvg(目标,s)
//空参数:将页面上的所有“svg”元素替换为“canvas”元素
//目标:画布元素或画布元素的id
//s:svg字符串、svg文件的url或xml文档
//选项:选项的可选哈希
//ignoreMouse:true=>忽略鼠标事件
//ignoreAnimation:true=>忽略动画
//ignoreDimensions:true=>不尝试调整画布大小
//ignoreClear:true=>不清除画布
//偏移量x:int=>以x偏移量绘制
//offsetY:int=>在y偏移处绘制
//scaleWidth:int=>水平缩放到宽度
//scaleHeight:int=>垂直缩放到高度
//renderCallback:function=>将在第一次渲染完成后调用该函数
//forceRedraw:function=>将在每一帧上调用该函数,如果返回true,将重新绘制
this.canvg=函数(目标、选项){
//无参数
if(target==null&&s==null&&opts==null){
var svgTags=document.querySelectorAll('svg');
对于(var i=0;i,我建议检查“s”是什么。它实际上可能不是字符串,因此执行“s.substr(0,1)”将是错误中引用的“未定义的”。谢谢TJ-您的观点帮助我找到了正确的方向
Canvg似乎需要一个字符串或html输入,而不是一个对象。因此,这是可行的:
var canvas = document.getElementById("canvas");
var svg = document.getElementById("div#forSVG");
var svgWider = svg.innerHTML;
canvg(canvas, svgWider);
如果问题发生在canvg.js:50附近,那么我建议发布canvg.js。在没有什么可看的情况下,很难帮助诊断问题。如果你的回复帮助你解决了问题,你可以(而且应该)将自己的回复标记为解决方案。这将标记其他人也解决了这个问题。
/*
* canvg.js - Javascript SVG parser and renderer on Canvas
* MIT Licensed
* Gabe Lerner (gabelerner@gmail.com)
* http://code.google.com/p/canvg/
*
* Requires: rgbcolor.js - http://www.phpied.com/rgb-color-parser-in-javascript/
*/
(function(){
// canvg(target, s)
// empty parameters: replace all 'svg' elements on page with 'canvas' elements
// target: canvas element or the id of a canvas element
// s: svg string, url to svg file, or xml document
// opts: optional hash of options
// ignoreMouse: true => ignore mouse events
// ignoreAnimation: true => ignore animations
// ignoreDimensions: true => does not try to resize canvas
// ignoreClear: true => does not clear canvas
// offsetX: int => draws at a x offset
// offsetY: int => draws at a y offset
// scaleWidth: int => scales horizontally to width
// scaleHeight: int => scales vertically to height
// renderCallback: function => will call the function after the first render is completed
// forceRedraw: function => will call the function on every frame, if it returns true, will redraw
this.canvg = function (target, s, opts) {
// no parameters
if (target == null && s == null && opts == null) {
var svgTags = document.querySelectorAll('svg');
for (var i=0; i<svgTags.length; i++) {
var svgTag = svgTags[i];
var c = document.createElement('canvas');
c.width = svgTag.clientWidth;
c.height = svgTag.clientHeight;
svgTag.parentNode.insertBefore(c, svgTag);
svgTag.parentNode.removeChild(svgTag);
var div = document.createElement('div');
div.appendChild(svgTag);
canvg(c, div.innerHTML);
}
return;
}
if (typeof target == 'string') {
target = document.getElementById(target);
}
// store class on canvas
if (target.svg != null) target.svg.stop();
var svg = build(opts || {});
// on i.e. 8 for flash canvas, we can't assign the property so check for it
if (!(target.childNodes.length == 1 && target.childNodes[0].nodeName == 'OBJECT')) target.svg = svg;
var ctx = target.getContext('2d');
if (typeof(s.documentElement) != 'undefined') {
// load from xml doc
svg.loadXmlDoc(ctx, s);
}
else if (s.substr(0,1) == '<') {
// load from xml string
svg.loadXml(ctx, s);
}
else {
// load from url
svg.load(ctx, s);
}
}
var canvas = document.getElementById("canvas");
var svg = document.getElementById("div#forSVG");
var svgWider = svg.innerHTML;
canvg(canvas, svgWider);