@@ -4,26 +4,31 @@ import { hasFired } from './promisify.js'
44const canvas = document . createElement ( 'canvas' )
55const canvasCtx = canvas . getContext ( '2d' )
66
7- export function imageDataFromImage ( imageElement ) {
8- canvas . width = imageElement . naturalWidth
9- canvas . height = imageElement . naturalHeight
7+ canvas . width = 1920
8+ canvas . height = 1080
109
11- const bounds = [ 0 , 0 , canvas . width , canvas . height ]
10+ function imageDataFromCanvas ( canvasImageSource , width , height ) {
11+ const scalingRatio = Math . min ( 1 , canvas . width / width , canvas . height / height )
12+ const widthScaled = scalingRatio * width
13+ const heightScaled = scalingRatio * height
1214
13- canvasCtx . drawImage ( imageElement , ... bounds )
15+ canvasCtx . drawImage ( canvasImageSource , 0 , 0 , widthScaled , heightScaled )
1416
15- return canvasCtx . getImageData ( ... bounds )
17+ return canvasCtx . getImageData ( 0 , 0 , widthScaled , heightScaled )
1618}
1719
18- export function imageDataFromVideo ( videoElement ) {
19- canvas . width = videoElement . videoWidth
20- canvas . height = videoElement . videoHeight
20+ export function imageDataFromImage ( imageElement ) {
21+ const width = imageElement . naturalWidth
22+ const height = imageElement . naturalHeight
2123
22- const bounds = [ 0 , 0 , canvas . width , canvas . height ]
24+ return imageDataFromCanvas ( imageElement , width , height )
25+ }
2326
24- canvasCtx . drawImage ( videoElement , ...bounds )
27+ export function imageDataFromVideo ( videoElement ) {
28+ const width = videoElement . videoWidth
29+ const height = videoElement . videoHeight
2530
26- return canvasCtx . getImageData ( ... bounds )
31+ return imageDataFromCanvas ( videoElement , width , height )
2732}
2833
2934export async function imageDataFromUrl ( url ) {
0 commit comments