suryanshsingh2001 / mockly

Mockly is an open-source tool that allows developers to create professional-looking screenshots and mockups with ease. It's designed for fast, stress-free mockups for developers who'd rather code than design.
https://www.mockly.site/
MIT License
5 stars 15 forks source link

Fix canvas flickering #15

Closed kvtemadden closed 1 month ago

kvtemadden commented 1 month ago

Description

Used requestAnimationFrame to handle how often the browser is painting new content, removing the flicker when dragging the uploaded image.

This introduced problems with the backgroundImage though, so I've moved that to its own handler and ensured the backgroundImage is loaded before redrawing it back onto the canvas because simply using img.onload didn't seem to be managing it well enough.

Also tidied up a little bit of related code to reduce lines of code :)

Related Issue

Canvas flickering issue - 1

Closes # 1

Changes Made

Type of Change

Screenshots (if applicable)

Screen Recording 2024-09-27 at 14 18 46

How Has This Been Tested?

Checklist

Additional Context

vercel[bot] commented 1 month ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
mockly ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 30, 2024 8:17am
suryanshsingh2001 commented 1 month ago

Hi @kvtemadden,

I can't express how grateful I am for this fix! The canvas flickering issue has been a real headache, and I was gearing up for what could have been weeks of late-night debugging. Your solution using requestAnimationFrame has saved me so much time and frustration. Seriously, thank you!

That being said, I did notice a couple of things that still need some attention:

  1. Border Radius Not Working: It looks like the border radius on the canvas isn’t applying correctly. The edges are still sharp instead of rounded.

  2. Tainted Canvas Error: When exporting the image (no matter the format), I’m getting a tainted canvas error, which is related to CORS. This can be resolved by adding this one-liner wherever an image is loaded:

    img.setAttribute("crossOrigin", "anonymous");

Also, with Hacktoberfest starting on October 1, if you'd like, we can count this PR as hacktoberfest-accepted. We could merge it on October 1 to ensure it qualifies. Let me know what you think!

Your work on this has been incredibly helpful, and I’m really thankful. These little bugs are the only things standing between us and a perfect solution, and I’d love to see your final touches before we merge it in.

kvtemadden commented 1 month ago

Hey @suryanshsingh2001, that'd be great re Hacktoberfest please!

Those were great spots, thanks for pointing them out.

I've just added the fixes for those two issues back in, let me know if there's anything else you spot :)

suryanshsingh2001 commented 1 month ago

Hey @kvtemadden ,

Awesome, thanks for knocking those out! I'll merge it as soon as Hacktoberfest kicks off. 🎉 Really appreciate you contributing to Mockly—you're officially a bug-squashing ninja! 🐛🔨

If you’ve got more ideas for how we can make Mockly a better product (so people don’t have to jump into Canva or Figma for those clean screenshots), feel free to check out other issues or even add one yourself!

Let's keep crushing it! 💪

Cheers!

suryanshsingh2001 commented 1 month ago

Summary

This pull request to components/shared/Editor.tsx includes several updates to improve image handling and optimize the canvas rendering process. The most important changes include adding state management for loaded images and background images, refactoring the canvas drawing logic, and removing unnecessary code.

Image Handling Improvements:

Canvas Rendering Optimization:

Code Cleanup: