Skip to content

Commit

Permalink
refactor: Update video element resizing logic in Artplayer and artpla…
Browse files Browse the repository at this point in the history
…yer-proxy-webav
  • Loading branch information
zhw2590582 committed Aug 21, 2024
1 parent b711291 commit 5bbbaa3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
22 changes: 22 additions & 0 deletions docs/uncompiled/artplayer-proxy-webav/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/uncompiled/artplayer-proxy-webav/index.js.map

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions packages/artplayer-proxy-webav/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function artplayerProxyWebAV() {

const canvas = createElement('canvas');
const ctx = canvas.getContext('2d');

let audioCtx;
let gainNode;

Expand Down Expand Up @@ -149,6 +150,31 @@ export default function artplayerProxyWebAV() {
}
}

function resize() {
const player = art.template?.$player;
if (!player || option.autoSize) return;

const aspectRatio = canvas.videoWidth / canvas.videoHeight;
const containerWidth = player.clientWidth;
const containerHeight = player.clientHeight;
const containerRatio = containerWidth / containerHeight;

let paddingLeft = 0;
let paddingTop = 0;

if (containerRatio > aspectRatio) {
const canvasWidth = containerHeight * aspectRatio;
paddingLeft = (containerWidth - canvasWidth) / 2;
} else {
const canvasHeight = containerWidth / aspectRatio;
paddingTop = (containerHeight - canvasHeight) / 2;
}

Object.assign(canvas.style, {
padding: `${paddingTop}px ${paddingLeft}px`,
});
}

async function init() {
stop();
reset();
Expand Down Expand Up @@ -186,6 +212,7 @@ export default function artplayerProxyWebAV() {
canvas.width = state.videoWidth;
canvas.height = state.videoHeight;
await preview(0.1);
resize();

art.emit('video:loadedmetadata', { type: 'loadedmetadata' });
art.emit('video:durationchange', { type: 'durationchange' });
Expand Down Expand Up @@ -326,6 +353,8 @@ export default function artplayerProxyWebAV() {
}
});

art.on('resize', resize);

return canvas;
};
}
Expand Down

0 comments on commit 5bbbaa3

Please sign in to comment.