HTML5外部图像制作svg,html5-canvas

Example

To draw a vector SVG image, the operation is not different from a raster image :

You first need to load your SVG image into an HTMLImage element, then use the drawImage() method.

var image = new Image();

image.onload = function(){

ctx.drawImage(this, 0,0);

}

image.src = "someFile.SVG";

SVG images have some advantages over raster ones, since you won't loose quality, whatever the scale you'll draw it on your canvas. But beware, it may also be a bit slower than drawing a raster image.

However, SVG images come with more restrictions than raster images.

For security purpose, no external content can be loaded from an SVG image referenced in an HTMLImageElement()

No external stylesheet, no external image referenced in SVGImage () elements, no external filter or element linked by the xlink:href attribute () or the funcIRI (url()) attribute method etc.

Also, stylesheets appended in the main document won't have any effect on the SVG document once referenced in an HTMLImage element.

Finally, no script will be executed inside the SVG Image.

Workaround : You'll need to append all external elements inside the SVG itself before referrencing to the HTMLImage element. (for images or fonts, you need to append a dataURI version of your external resources).

The root element () must have its width and height attributes set to an absolute value.

If you were to use relative length (e.g %), then the browser won't be able to know to what it is relative.

Some browsers (Blink) will try to make a guess, but most will simply ignore your image and won't draw anything, without a warning.

Some browsers will taint the canvas when an SVG image has been drawn to it.

Specifically, Internet-Explorer < Edge in any case, and Safari 9 when a is present in the SVG image.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值