-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
组件中使用,setCanvas 走不通 #8
Comments
我也遇到了同样的问题 |
有问题要给 demo,代码片段或者zip包都可以,没有 demo 是不会解问题的。 |
我的解决办法是在外面加上一个Timer 解决办法如下: //方案1 神戳戳的异步 (早期写的,突然醒悟看了源码不推荐这样写 )
let timer=setTimeout(() => {
clearTimeout(timer);
(async () => {
let parser = new Parser();
let player = new Player();
let mySvga = await parser.load(`forTest.svga`); //让他先下载再加载canvas 减小错误的概率
await player.setCanvas(`#canvas`);
await player.setVideoItem(mySvga);
player.loops = 1;
player.clearsAfterStop = false;
player.startAnimation();
player.onFinished(()=>{
console.log("动画播放完成");
});
})();
}, 1000); //定时器时间随意,和手机性能有关系
//方案2 完美解决setCanvas失败的问题,需要优先渲染出<canvas/>组件
let parser = new Parser();
let mySvga = await parser.load(`forTest.svga`);
let player = new Player();
player.setCanvas(`#${canvas}`)
.then(res => {
if (loopTimes) player.loops = loopTimes;
player.setVideoItem(mySvga);
player.clearsAfterStop = false;
player.startAnimation();
});
<Canvas type='2d' id='festivalSvgaCanvas' />
showSvga('festivalSvgaCanvas', 'svgaName', 1)
/**
* 预下载Svga图片
* @param {*} svgaName
*/
export async function preLoadSvga(svgaName = '', callback = undefined) {
let parser = new Parser();
let localSvga = svgaCollection[svgaName];
if (!localSvga) {
localSvga = await parser.load(`https://cdn.baidu.com/${svgaName}.svga`);
svgaCollection[svgaName] = localSvga;
}
if (callback) callback(localSvga);
}
/**
* 显示Svga到指定的canvas组件上
* @param {*} canvasName
* @param {*} svgaName
* @param {*} loopTimes
* @param {*} succback 成功回调
* @param {*} failback 失败回调
*/
export function showSvga(canvasName, svgaName, loopTimes, succback, failback, clearsAfterStop = false) {
let interval = setInterval(() => {
let query = wx.createSelectorQuery();
query.select(`#${canvasName}`).boundingClientRect()
query.exec(function (res) { // exec 返回 NodesRef 对象
//判断组件存在
if (res && res[0]) {
clearInterval(interval);
preLoadSvga(svgaName, async (mySvga) => {
let player = new Player();
try {
player.setCanvas(`#${canvasName}`)
.then(res => {
try {
if (loopTimes) player.loops = loopTimes;
player.clearsAfterStop = clearsAfterStop;
player.setVideoItem(mySvga);
player.startAnimation();
player.onFinished(() => {
console.log("动画播放完成");
if (succback) succback();
});
} catch (error) {
if (failback) failback();
}
});
} catch (error) {
if (failback) failback();
}
});
} else {
console.log('没有找到canvas')
}
})
}, 200);
}```
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
canvas定义在组件中使用,按照给的例子,在 setCanvas() 传入了当前实例,只能打印出“11111”,没有“222222”,有没有知道在组件中怎么使用的呢?
The text was updated successfully, but these errors were encountered: