-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
55 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
export function drawSequenceFrame(url: string, canvas: HTMLCanvasElement): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
if (!canvas) { | ||
reject(new Error('Canvas element not found!')) | ||
return | ||
} | ||
|
||
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D | ||
|
||
const img = new Image() | ||
img.onload = () => { | ||
// 设置canvas尺寸 | ||
canvas.width = canvas.clientWidth | ||
canvas.height = canvas.clientHeight | ||
|
||
// 图片比例和canvas比例 | ||
const imgRatio = img.width / img.height | ||
const canvasRatio = canvas.width / canvas.height | ||
|
||
let drawWidth, drawHeight, drawX, drawY | ||
|
||
// 根据比例确定绘制尺寸和位置 | ||
if (imgRatio > canvasRatio) { | ||
drawWidth = canvas.width | ||
drawHeight = canvas.width / imgRatio | ||
drawX = 0 | ||
drawY = (canvas.height - drawHeight) / 2 | ||
} | ||
else { | ||
drawWidth = canvas.height * imgRatio | ||
drawHeight = canvas.height | ||
drawX = (canvas.width - drawWidth) / 2 | ||
drawY = 0 | ||
} | ||
|
||
// 清除之前的内容 | ||
ctx.clearRect(0, 0, canvas.width, canvas.height) | ||
|
||
// 绘制图片 | ||
ctx.drawImage(img, drawX, drawY, drawWidth, drawHeight - 15) | ||
|
||
resolve() | ||
} | ||
|
||
img.onerror = () => { | ||
reject(new Error('Failed to load image')) | ||
} | ||
|
||
img.src = url | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './date' | ||
export * from './storage' | ||
export * from './mock' | ||
export * from './canvas' |