Skip to content

Conversation

@Atharva-Kanherkar
Copy link
Contributor

This PR adds support for the Blend algebra to the Canvas backend.

Implementation:

  1. Canvas Blend Implementation:

    • Added CanvasBlend trait in canvas/src/main/scala/doodle/canvas/algebra/CanvasBlend.scala
    • Implemented all methods required by the Blend trait using Canvas's globalCompositeOperation
    • Used a private blend helper function to avoid code duplication
  2. How it works:

    • Each blend method uses the Finalized and Renderable pattern to wrap the original drawing operation
    • The wrapper saves the canvas context, sets the appropriate blend mode, runs the original drawing, then restores the context
    • This preserves all other drawing state while applying the blend effect

Testing:

I noted that the golden test infrastructure uses Java2D (which doesn't support the Blend trait), so I haven't included automated tests.

Based on the discussion in the issue, I understand that:

  1. Automated visual testing is only available for Java2D
  2. Canvas blend modes should be tested through example pages in documentation
  3. Adding Blend to Java2D would be a separate issue (Implement blending modes for Java2D #191)

I'd appreciate feedback on this implementation, particularly on:

  1. The approach used to wrap the original drawing functions
  2. Suggestions for appropriate testing strategies

@Atharva-Kanherkar Atharva-Kanherkar marked this pull request as draft May 5, 2025 17:58
@Atharva-Kanherkar
Copy link
Contributor Author

Atharva-Kanherkar commented May 12, 2025

hi @noelwelsh , would love to receive your feedback on this!

Copy link
Contributor

@noelwelsh noelwelsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small changes to follow the existing conventions.

@Atharva-Kanherkar
Copy link
Contributor Author

Hey @noelwelsh , I have added the requested changes!

@Atharva-Kanherkar Atharva-Kanherkar marked this pull request as ready for review May 13, 2025 20:47
Copy link
Contributor

@noelwelsh noelwelsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small changes.

blendMode: String
)(drawing: CanvasDrawing[A]): CanvasDrawing[A] = {
CanvasDrawing { ctx =>
ctx.save()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saving and restoring the canvas state is quite expensive (there is a lot of state). Better to just save the current blend mode (i.e. the value of globalCompositeOperation).

@Atharva-Kanherkar
Copy link
Contributor Author

@noelwelsh sure ill get back to you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants