Skip to content

Commit

Permalink
Merge pull request #185 from lucky980421/feature/update-decode-image-…
Browse files Browse the repository at this point in the history
…demo

Feature/update decode image demo
  • Loading branch information
hughfenghen authored Jul 18, 2024
2 parents 542f5da + 2ea4d57 commit e8ad40c
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions doc-site/docs/demo/decode-image.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decodeImg } from '@webav/av-cliper';
import { ImgClip } from '@webav/av-cliper';
import { Button, Radio } from 'antd';
import React, { useState } from 'react';
import { assetsPrefix } from './utils';
Expand All @@ -15,31 +15,29 @@ async function start(
ctx: CanvasRenderingContext2D,
imgType: keyof typeof imgs,
) {
const frames = await decodeImg(
(await fetch(imgs[imgType])).body!,
`image/${imgType}`,
);

const clip = new ImgClip({
type: `image/${imgType}`,
stream: (await fetch(imgs[imgType])).body!,
});
await clip.ready;
stopRender();
let i = 0;
function render(vf: VideoFrame) {
if (vf == null) return;
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.drawImage(vf, 0, 0);

const timer = setTimeout(
() => {
render(frames[++i]);
vf.close();
},
(vf.duration ?? 0) / 1000,
);
function render() {
let startTime = performance.now();
const timer = setInterval(async () => {
const { video } = await clip.tick((performance.now() - startTime) * 1000);
if (video != null) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.drawImage(video, 0, 0);
video.close();
}
}, 1000 / 30);

stopRender = () => {
clearTimeout(timer);
clip.destroy();
clearInterval(timer);
};
}
render(frames[0]);
render();
}

export default createUI(start);
Expand Down

0 comments on commit e8ad40c

Please sign in to comment.