Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript绘制SVG_Javascript_Html_Canvas_Svg - Fatal编程技术网

使用JavaScript绘制SVG

使用JavaScript绘制SVG,javascript,html,canvas,svg,Javascript,Html,Canvas,Svg,我试图重新创建背景动画,但我想使用三个不同的SVG文件,而不是使用javascript绘制的随机三角形 我试图通过本文复制这个函数,但没有完全正确 所以我的主要问题是,我需要如何以及在哪里操作脚本来绘制SVG,而不是它现在正在绘制的三角形。或者如果可能的话 我的猜测可能与脚本的这一部分有关,但我不能100%确定: this.draw = function() { if(_this.alpha >= 0.005) _this.alpha -= 0.005; else _thi

我试图重新创建背景动画,但我想使用三个不同的SVG文件,而不是使用javascript绘制的随机三角形

我试图通过本文复制这个函数,但没有完全正确

所以我的主要问题是,我需要如何以及在哪里操作脚本来绘制SVG,而不是它现在正在绘制的三角形。或者如果可能的话

我的猜测可能与脚本的这一部分有关,但我不能100%确定:

this.draw = function() {
    if(_this.alpha >= 0.005) _this.alpha -= 0.005;
    else _this.alpha = 0;
    ctx.beginPath();
    ctx.moveTo(_this.coords[0].x+_this.pos.x, _this.coords[0].y+_this.pos.y);
    ctx.lineTo(_this.coords[1].x+_this.pos.x, _this.coords[1].y+_this.pos.y);
    ctx.lineTo(_this.coords[2].x+_this.pos.x, _this.coords[2].y+_this.pos.y);
    ctx.closePath();
    ctx.fillStyle = 'rgba('+_this.color+','+ _this.alpha+')';
    ctx.fill();
};

提前感谢您的帮助,如果需要进一步澄清问题,请告诉我

您可以在每次谈论svg时使用随机颜色和宽度/高度(当然还有您想要随机的任何其他东西)重新绘制相同的svg,但要发布画布代码。。我在这里遗漏了什么吗?根据你想使用ctx的问题中链接的那篇文章。drawImage@RobertLongson我的问题是,我应该在哪里向脚本中添加这些代码?替换所有现有的ctx。使用ctx.drawImage调用调用。在这一点上,你真的想要一本画布上的书,因为很难通过问答的方式从零开始学习。