-
-
Notifications
You must be signed in to change notification settings - Fork 364
feat(Utility): add drawImage method #5938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -876,6 +876,16 @@ export function setMemorialMode(memorial) { | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| export function drawImage(canvas, image, offsetWidth, offsetHeight) { | ||||||||||||||||||||||||||||
| canvas.width = offsetWidth * devicePixelRatio; | ||||||||||||||||||||||||||||
| canvas.height = offsetHeight * devicePixelRatio; | ||||||||||||||||||||||||||||
| canvas.style.width = `${offsetWidth}px`; | ||||||||||||||||||||||||||||
| canvas.style.height = `${offsetHeight}px`; | ||||||||||||||||||||||||||||
| const context = canvas.getContext('2d'); | ||||||||||||||||||||||||||||
| context.scale(devicePixelRatio, devicePixelRatio); | ||||||||||||||||||||||||||||
|
Comment on lines
+880
to
+885
|
||||||||||||||||||||||||||||
| canvas.width = offsetWidth * devicePixelRatio; | |
| canvas.height = offsetHeight * devicePixelRatio; | |
| canvas.style.width = `${offsetWidth}px`; | |
| canvas.style.height = `${offsetHeight}px`; | |
| const context = canvas.getContext('2d'); | |
| context.scale(devicePixelRatio, devicePixelRatio); | |
| const pixelRatio = window.devicePixelRatio; | |
| canvas.width = offsetWidth * pixelRatio; | |
| canvas.height = offsetHeight * pixelRatio; | |
| canvas.style.width = `${offsetWidth}px`; | |
| canvas.style.height = `${offsetHeight}px`; | |
| const context = canvas.getContext('2d'); | |
| context.scale(pixelRatio, pixelRatio); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Handle potential null from getContext('2d').
Verify canvas.getContext('2d') returns a non-null context before scaling or drawing the image.
| const context = canvas.getContext('2d'); | |
| context.scale(devicePixelRatio, devicePixelRatio); | |
| context.drawImage(image, 0, 0, offsetWidth, offsetHeight); | |
| const context = canvas.getContext('2d'); | |
| if (context) { | |
| context.scale(devicePixelRatio, devicePixelRatio); | |
| context.drawImage(image, 0, 0, offsetWidth, offsetHeight); | |
| } else { | |
| console.error('2D context not available on canvas.'); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider verifying that canvas.getContext('2d') returns a valid context object before applying transformations and drawing the image.