在特定时间在HTML5画布上绘制SVG文件 [英] Drawing a SVG file on a HTML5 canvas at a specific time

查看:86
本文介绍了在特定时间在HTML5画布上绘制SVG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了主题在HTML5画布上绘制SVG文件有关渲染SVG.有没有办法在特定时间绘制SVG动画文件?我的意思是,如何更改代码

I found the topic Drawing an SVG file on a HTML5 canvas about rendering SVG. Is there a way to draw SVG animated file at specific moment of time? I mean, how to change the code

var img = new Image();
img.onload = function() {
    ctx.drawImage(img, 0, 0); // define time at here: time="1.3sec"
}
img.src = "myfile.svg";

例如img在特定时间包含svg? 文件myfile.svg包含以下内容:

such that img contains svg at specific time? The file myfile.svg contains the following:

<svg
   xmlns="http://www.w3.org/2000/svg"
   viewBox="0 0 200 200"
   id="svg2">

    <g transform="translate(100, 100)">
        <path transform="matrix(0.99982594,0.01865711,-0.01865711,0.99982594,0,0)" style="opacity:1;fill:#28b327;fill-opacity:1;stroke:#000000;stroke-width:1.5;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" d="M 23.755845,-0.77020556 A 23.938168,23.938168 0 0 1 -0.1823229,23.167962 23.938168,23.938168 0 0 1 -24.12049,-0.77020556 23.938168,23.938168 0 0 1 -0.1823229,-24.708373 23.938168,23.938168 0 0 1 23.755845,-0.77020556 Z" id="ball-2-path4136">
        <animate to="M 17.577751,-4.4423325 A 17.748762,27.248762 1.0690357 0 1 -0.67630452,22.470547 17.748762,27.248762 1.0690357 0 1 -17.913594,-5.1046137 17.748762,27.248762 1.0690357 0 1 0.3404618,-32.017493 17.748762,27.248762 1.0690357 0 1 17.577751,-4.4423325 Z" dur="0.2" begin="1.4" attributeName="d" fill="freeze"/></path>

    </g>
</svg>

人们通常建议使用canvg,但据我了解,它不支持路径动画,因此我的示例无法使用它.

People usually suggest to use canvg, but as far as I understood it does not support path animation, so my example does not work with it.

推荐答案

您可以简单地使用setTimeout以此方式延迟正方形的插入:

You can simply use setTimeout to defer the insertion of the square, in this fashion :

var delayInMilliseconds = 3000;

img.onload = function() {
    setTimeout(function() {
        // This will happend 3 seconds after page load
        ctx.drawImage(img, 0, 0);
    }, delayInMilliseconds);
}
img.src = "http://upload.wikimedia.org/wikipedia/commons/d/d2/Svg_example_square.svg";

如果您希望能够指定事件的日期和时间,请使用自定义函数来计算到目标日期的剩余时间:

In case you want to be able to specify a date and time for the event, use a custom function to count the time left to the target date :

function getTimeTo(date) {
    var time = date.getTime() - Date.now();
    return time;
}

,然后将delayInMilliseconds替换为getTimeTo(targetDate).有关实验,请参见此小提琴.

And replace delayInMilliseconds with getTimeTo(targetDate). See this fiddle for experimenting.

这篇关于在特定时间在HTML5画布上绘制SVG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆