diff --git a/docs/uncompiled/artplayer-proxy-canvas/index.js b/docs/uncompiled/artplayer-proxy-canvas/index.js index d96b801c6..0e6c830d8 100644 --- a/docs/uncompiled/artplayer-proxy-canvas/index.js +++ b/docs/uncompiled/artplayer-proxy-canvas/index.js @@ -261,11 +261,8 @@ function artplayerProxyCanvas(callback) { art.on("video:loadedmetadata", async ()=>{ canvas.width = video.videoWidth; canvas.height = video.videoHeight; - resize(); - }); - // 在视频数据可用时绘制第一帧 - art.on("video:canplay", async ()=>{ await sleep(300); + resize(); drawFrame(); }); art.on("video:play", ()=>{ diff --git a/docs/uncompiled/artplayer-proxy-canvas/index.js.map b/docs/uncompiled/artplayer-proxy-canvas/index.js.map index 154289f11..561f38dad 100644 --- a/docs/uncompiled/artplayer-proxy-canvas/index.js.map +++ b/docs/uncompiled/artplayer-proxy-canvas/index.js.map @@ -1 +1 @@ -{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6CAAwB;AAAT,SAAS,qBAAqB,QAAQ;IACjD,OAAO,CAAC;QACJ,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG;QAChC,MAAM,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,YAAY,KAAK;QAE/D,MAAM,SAAS,cAAc;QAC7B,MAAM,MAAM,OAAO,UAAU,CAAC;QAC9B,MAAM,QAAQ,cAAc;QAC5B,MAAM,QAAQ,cAAc;QAC5B,MAAM,OAAO,GAAG;QAChB,MAAM,IAAI,GAAG;QACb,OAAO,OAAO;QAEd,IAAI,iBAAiB;QACrB,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,YAAY,MAAM;QACrE,MAAM,OAAO;eAAI;eAAc;e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U,IAAI,EAAE,QAAQ,EAAE,GAAG;IAC5C,OAAO,cAAc,CAAC,MAAM,UAAU;QACpC,YAAY;QACZ,KAAK;IACP;AACF","sources":["packages/artplayer-proxy-canvas/src/index.js","node_modules/@parcel/transformer-js/src/esmodule-helpers.js"],"sourcesContent":["export default function artplayerProxyCanvas(callback) {\n return (art) => {\n const { option, constructor } = art;\n const { createElement, def, append, sleep } = constructor.utils;\n\n const canvas = createElement('canvas');\n const ctx = canvas.getContext('2d');\n const video = createElement('video');\n const track = createElement('track');\n track.default = true;\n track.kind = 'metadata';\n append(video, track);\n\n let animationFrame = null;\n const { propertys, methods, prototypes, events } = constructor.config;\n const keys = [...propertys, ...methods, ...prototypes];\n\n // 存储 canvas 的原始属性\n const originalCanvasProperties = {};\n ['width', 'height'].forEach((prop) => {\n originalCanvasProperties[prop] = Object.getOwnPropertyDescriptor(HTMLCanvasElement.prototype, prop);\n });\n\n keys.forEach((key) => {\n if (key === 'width' || key === 'height') {\n def(canvas, key, {\n get() {\n return originalCanvasProperties[key].get.call(this);\n },\n set(value) {\n originalCanvasProperties[key].set.call(this, value);\n video[key] = value;\n },\n });\n } else {\n def(canvas, key, {\n get() {\n const value = video[key];\n return typeof value === 'function' ? value.bind(video) : value;\n },\n set(value) {\n video[key] = value;\n },\n });\n }\n });\n\n setTimeout(() => {\n for (let index = 0; index < events.length; index++) {\n const event = events[index];\n art.proxy(video, event, (event) => {\n art.emit(`video:${event.type}`, event);\n });\n }\n });\n\n const drawFrame = async () => {\n if (!video.videoWidth || !video.videoHeight) {\n console.log('Video dimensions not ready yet');\n return;\n }\n try {\n if (video.readyState >= 2) {\n // HAVE_CURRENT_DATA or higher\n // 使用 createImageBitmap 进行预处理\n const bitmap = await createImageBitmap(video);\n ctx.drawImage(bitmap, 0, 0, canvas.width, canvas.height);\n bitmap.close();\n } else {\n console.log('Video not ready for frame extraction');\n }\n } catch (error) {\n console.error('Error drawing video frame:', error);\n }\n };\n\n // 使用 requestVideoFrameCallback 进行精确同步\n let videoFrameCallback;\n if ('requestVideoFrameCallback' in HTMLVideoElement.prototype) {\n videoFrameCallback = () => {\n drawFrame();\n video.requestVideoFrameCallback(videoFrameCallback);\n };\n } else {\n // 降级方案:使用 requestAnimationFrame\n videoFrameCallback = () => {\n drawFrame();\n animationFrame = requestAnimationFrame(videoFrameCallback);\n };\n }\n\n const resize = () => {\n try {\n const player = art.template?.$player;\n if (!player || option.autoSize) return;\n\n const aspectRatio = video.videoWidth / video.videoHeight;\n const containerWidth = player.clientWidth;\n const containerHeight = player.clientHeight;\n const containerRatio = containerWidth / containerHeight;\n\n let canvasWidth, canvasHeight;\n if (containerRatio > aspectRatio) {\n canvasHeight = containerHeight;\n canvasWidth = canvasHeight * aspectRatio;\n } else {\n canvasWidth = containerWidth;\n canvasHeight = canvasWidth / aspectRatio;\n }\n\n // 设置 canvas 大小为实际显示大小\n canvas.width = canvasWidth;\n canvas.height = canvasHeight;\n\n // 居中 canvas\n const paddingLeft = (containerWidth - canvasWidth) / 2;\n const paddingTop = (containerHeight - canvasHeight) / 2;\n\n Object.assign(canvas.style, {\n padding: `${paddingTop}px ${paddingLeft}px`,\n });\n } catch (error) {\n console.error('Error in resize function:', error);\n }\n };\n\n // 在视频元数据加载完成后设置 canvas 尺寸并绘制第一帧\n art.on('video:loadedmetadata', async () => {\n canvas.width = video.videoWidth;\n canvas.height = video.videoHeight;\n resize();\n });\n\n // 在视频数据可用时绘制第一帧\n art.on('video:canplay', async () => {\n await sleep(300);\n drawFrame();\n });\n\n art.on('video:play', () => {\n if ('requestVideoFrameCallback' in HTMLVideoElement.prototype) {\n video.requestVideoFrameCallback(videoFrameCallback);\n } else {\n cancelAnimationFrame(animationFrame);\n animationFrame = requestAnimationFrame(videoFrameCallback);\n }\n });\n\n art.on('video:pause', () => {\n if (!('requestVideoFrameCallback' in HTMLVideoElement.prototype)) {\n cancelAnimationFrame(animationFrame);\n }\n });\n\n art.on('resize', resize);\n\n const destroy = () => {\n if ('requestVideoFrameCallback' in HTMLVideoElement.prototype) {\n video.cancelVideoFrameCallback(videoFrameCallback);\n } else {\n cancelAnimationFrame(animationFrame);\n }\n // 清理其他可能的资源...\n };\n\n art.on('destroy', destroy);\n\n if (typeof callback === 'function') {\n callback.call(art, video, option.url, art);\n } else {\n video.src = option.url;\n }\n\n return canvas;\n };\n}\n\nif (typeof window !== 'undefined') {\n window['artplayerProxyCanvas'] = artplayerProxyCanvas;\n}\n","exports.interopDefault = function (a) {\n return a && a.__esModule ? a : {default: a};\n};\n\nexports.defineInteropFlag = function (a) {\n Object.defineProperty(a, '__esModule', {value: true});\n};\n\nexports.exportAll = function (source, dest) {\n Object.keys(source).forEach(function (key) {\n if (\n key === 'default' ||\n key === '__esModule' ||\n Object.prototype.hasOwnProperty.call(dest, key)\n ) {\n return;\n }\n\n Object.defineProperty(dest, key, {\n enumerable: true,\n get: function () {\n return source[key];\n },\n });\n });\n\n return dest;\n};\n\nexports.export = function (dest, destName, get) {\n Object.defineProperty(dest, destName, {\n enumerable: true,\n get: get,\n });\n};\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../../../"} \ No newline at end of file +{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6CAAwB;AAAT,SAAS,qBAAqB,QAAQ;IACjD,OAAO,CAAC;QACJ,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG;QAChC,MAAM,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,YAAY,KAAK;QAE/D,MAAM,SAAS,cAAc;QAC7B,MAAM,MAAM,OAAO,UAAU,CAAC;QAC9B,MAAM,QAAQ,cAAc;QAC5B,MAAM,QAAQ,cAAc;QAC5B,MAAM,OAAO,GAAG;QAChB,MAAM,IAAI,G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;YACZ,KAAK;gBACH,OAAO,MAAM,CAAC,IAAI;YACpB;QACF;IACF;IAEA,OAAO;AACT;AAEA,QAAQ,MAAM,GAAG,SAAU,IAAI,EAAE,QAAQ,EAAE,GAAG;IAC5C,OAAO,cAAc,CAAC,MAAM,UAAU;QACpC,YAAY;QACZ,KAAK;IACP;AACF","sources":["packages/artplayer-proxy-canvas/src/index.js","node_modules/@parcel/transformer-js/src/esmodule-helpers.js"],"sourcesContent":["export default function artplayerProxyCanvas(callback) {\n return (art) => {\n const { option, constructor } = art;\n const { createElement, def, append, sleep } = constructor.utils;\n\n const canvas = createElement('canvas');\n const ctx = canvas.getContext('2d');\n const video = createElement('video');\n const track = createElement('track');\n track.default = true;\n track.kind = 'metadata';\n append(video, track);\n\n let animationFrame = null;\n const { propertys, methods, prototypes, events } = constructor.config;\n const keys = [...propertys, ...methods, ...prototypes];\n\n // 存储 canvas 的原始属性\n const originalCanvasProperties = {};\n ['width', 'height'].forEach((prop) => {\n originalCanvasProperties[prop] = Object.getOwnPropertyDescriptor(HTMLCanvasElement.prototype, prop);\n });\n\n keys.forEach((key) => {\n if (key === 'width' || key === 'height') {\n def(canvas, key, {\n get() {\n return originalCanvasProperties[key].get.call(this);\n },\n set(value) {\n originalCanvasProperties[key].set.call(this, value);\n video[key] = value;\n },\n });\n } else {\n def(canvas, key, {\n get() {\n const value = video[key];\n return typeof value === 'function' ? value.bind(video) : value;\n },\n set(value) {\n video[key] = value;\n },\n });\n }\n });\n\n setTimeout(() => {\n for (let index = 0; index < events.length; index++) {\n const event = events[index];\n art.proxy(video, event, (event) => {\n art.emit(`video:${event.type}`, event);\n });\n }\n });\n\n const drawFrame = async () => {\n if (!video.videoWidth || !video.videoHeight) {\n console.log('Video dimensions not ready yet');\n return;\n }\n try {\n if (video.readyState >= 2) {\n // HAVE_CURRENT_DATA or higher\n // 使用 createImageBitmap 进行预处理\n const bitmap = await createImageBitmap(video);\n ctx.drawImage(bitmap, 0, 0, canvas.width, canvas.height);\n bitmap.close();\n } else {\n console.log('Video not ready for frame extraction');\n }\n } catch (error) {\n console.error('Error drawing video frame:', error);\n }\n };\n\n // 使用 requestVideoFrameCallback 进行精确同步\n let videoFrameCallback;\n if ('requestVideoFrameCallback' in HTMLVideoElement.prototype) {\n videoFrameCallback = () => {\n drawFrame();\n video.requestVideoFrameCallback(videoFrameCallback);\n };\n } else {\n // 降级方案:使用 requestAnimationFrame\n videoFrameCallback = () => {\n drawFrame();\n animationFrame = requestAnimationFrame(videoFrameCallback);\n };\n }\n\n const resize = () => {\n try {\n const player = art.template?.$player;\n if (!player || option.autoSize) return;\n\n const aspectRatio = video.videoWidth / video.videoHeight;\n const containerWidth = player.clientWidth;\n const containerHeight = player.clientHeight;\n const containerRatio = containerWidth / containerHeight;\n\n let canvasWidth, canvasHeight;\n if (containerRatio > aspectRatio) {\n canvasHeight = containerHeight;\n canvasWidth = canvasHeight * aspectRatio;\n } else {\n canvasWidth = containerWidth;\n canvasHeight = canvasWidth / aspectRatio;\n }\n\n // 设置 canvas 大小为实际显示大小\n canvas.width = canvasWidth;\n canvas.height = canvasHeight;\n\n // 居中 canvas\n const paddingLeft = (containerWidth - canvasWidth) / 2;\n const paddingTop = (containerHeight - canvasHeight) / 2;\n\n Object.assign(canvas.style, {\n padding: `${paddingTop}px ${paddingLeft}px`,\n });\n } catch (error) {\n console.error('Error in resize function:', error);\n }\n };\n\n // 在视频元数据加载完成后设置 canvas 尺寸并绘制第一帧\n art.on('video:loadedmetadata', async () => {\n canvas.width = video.videoWidth;\n canvas.height = video.videoHeight;\n await sleep(300);\n resize();\n drawFrame();\n });\n\n art.on('video:play', () => {\n if ('requestVideoFrameCallback' in HTMLVideoElement.prototype) {\n video.requestVideoFrameCallback(videoFrameCallback);\n } else {\n cancelAnimationFrame(animationFrame);\n animationFrame = requestAnimationFrame(videoFrameCallback);\n }\n });\n\n art.on('video:pause', () => {\n if (!('requestVideoFrameCallback' in HTMLVideoElement.prototype)) {\n cancelAnimationFrame(animationFrame);\n }\n });\n\n art.on('resize', resize);\n\n const destroy = () => {\n if ('requestVideoFrameCallback' in HTMLVideoElement.prototype) {\n video.cancelVideoFrameCallback(videoFrameCallback);\n } else {\n cancelAnimationFrame(animationFrame);\n }\n // 清理其他可能的资源...\n };\n\n art.on('destroy', destroy);\n\n if (typeof callback === 'function') {\n callback.call(art, video, option.url, art);\n } else {\n video.src = option.url;\n }\n\n return canvas;\n };\n}\n\nif (typeof window !== 'undefined') {\n window['artplayerProxyCanvas'] = artplayerProxyCanvas;\n}\n","exports.interopDefault = function (a) {\n return a && a.__esModule ? a : {default: a};\n};\n\nexports.defineInteropFlag = function (a) {\n Object.defineProperty(a, '__esModule', {value: true});\n};\n\nexports.exportAll = function (source, dest) {\n Object.keys(source).forEach(function (key) {\n if (\n key === 'default' ||\n key === '__esModule' ||\n Object.prototype.hasOwnProperty.call(dest, key)\n ) {\n return;\n }\n\n Object.defineProperty(dest, key, {\n enumerable: true,\n get: function () {\n return source[key];\n },\n });\n });\n\n return dest;\n};\n\nexports.export = function (dest, destName, get) {\n Object.defineProperty(dest, destName, {\n enumerable: true,\n get: get,\n });\n};\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../../../"} \ No newline at end of file diff --git a/packages/artplayer-proxy-canvas/src/index.js b/packages/artplayer-proxy-canvas/src/index.js index e91947f9c..7f9005240 100644 --- a/packages/artplayer-proxy-canvas/src/index.js +++ b/packages/artplayer-proxy-canvas/src/index.js @@ -128,12 +128,8 @@ export default function artplayerProxyCanvas(callback) { art.on('video:loadedmetadata', async () => { canvas.width = video.videoWidth; canvas.height = video.videoHeight; - resize(); - }); - - // 在视频数据可用时绘制第一帧 - art.on('video:canplay', async () => { await sleep(300); + resize(); drawFrame(); });