-
-
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
Conversation
Co-Authored-By: a0926019412 <[email protected]>
Reviewer's GuideThis pull request introduces a new JavaScript utility function File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Pull Request Overview
This PR updates the project version and introduces a new JavaScript utility function to draw an image on a canvas with proper scaling for device pixel ratios.
- Updated project version in BootstrapBlazor.csproj
- Added the drawImage function in utility.js
Files not reviewed (1)
- src/BootstrapBlazor/BootstrapBlazor.csproj: Language not supported
| canvas.height = offsetHeight * devicePixelRatio; | ||
| canvas.style.width = `${offsetWidth}px`; | ||
| canvas.style.height = `${offsetHeight}px`; | ||
| const context = canvas.getContext('2d'); |
Copilot
AI
May 2, 2025
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.
| const context = canvas.getContext('2d'); | |
| const context = canvas.getContext('2d'); | |
| if (!context) { | |
| console.error("Failed to get 2D context from canvas."); | |
| return; | |
| } |
| 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); |
Copilot
AI
May 2, 2025
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.
[nitpick] Consider assigning window.devicePixelRatio to a local variable if used multiple times for improved clarity and potential performance benefits.
| 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.
Hey @ArgoZhang - I've reviewed your changes - here's some feedback:
- The
drawImagefunction could be made more flexible by accepting an existing canvas context instead of retrieving it internally.
Here's what I looked at during the review
- 🟡 General issues: 1 issue found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| const context = canvas.getContext('2d'); | ||
| context.scale(devicePixelRatio, devicePixelRatio); | ||
| context.drawImage(image, 0, 0, offsetWidth, offsetHeight); |
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.'); | |
| } |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #5938 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 665 665
Lines 30485 30485
Branches 4345 4345
=========================================
Hits 30485 30485 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Link issues
fixes #5937
Summary By Copilot
This pull request includes a version update in the project file and adds a new utility function for drawing images on a canvas in the JavaScript module. Below are the details:
Project Version Update:
src/BootstrapBlazor/BootstrapBlazor.csproj: Updated the project version from9.6.1-beta01to9.6.1-beta02.JavaScript Utility Enhancement:
src/BootstrapBlazor/wwwroot/modules/utility.js: Added a newdrawImagefunction to handle drawing images on a canvas with proper scaling for device pixel ratios.Regression?
Risk
Verification
Packaging changes reviewed?
☑️ Self Check before Merge
Summary by Sourcery
Add a new utility function for drawing images on canvas with proper device pixel ratio scaling
New Features:
drawImagemethod in utility.js to handle canvas image drawing with device pixel ratio supportChores: