Clojure2D/clojure2d

Artifacts in saved images

rrrnld opened this issue · 4 comments

This is the drawing I see in the window:
2019-11-18-203108_300x400_scrot

This is what I get when saving the image:
2019-11-18--20-27-35

I noticed this before with images that have a lot of small lines. What is happening here? This is the code I use for saving:

(defmethod c2d/key-pressed ["Doodle Seven" \s] [_ _]
  (let [timestamp (.format (java.text.SimpleDateFormat. "yyyy-MM-dd--HH-mm-ss")
                           (java.util.Date.))
        name (str timestamp ".png")]
    (c2d/save (c2d/get-canvas sketch) name)))

PS: I removed the c2d/get-canvas call and am now calling save directly on sketch, which is my window. I tried that yesterday and also had a lot of tearing (complex canvas content with many lines), this now looks better but still a bit broken (notice the upper corner):
2019-11-18--20-33-53

The main reason why saving is tricky (or can be considered as broken) is that:

  • saving in events: event processing is separated and not synchronized with drawing loop. I didn't implement the approach from Quil/Processing where event handler is called after drawing and releasing graphical context. Which assures that everything with drawing is done.
  • saving in drawing loop: actually I haven't faced this issue. The graphical context is allocated in loop - this is only suspect. Could you share the code please?

Possible approaches:

  • If you don't want an animation, do not draw in loop. You don't need the drawing function to display stuff on the screen. See this example: https://github.com/Clojure2D/clojure2d-examples/blob/master/src/ex08_folds.clj#L80
    After calling (draw-folds (example-08)) you are guaranteed that graphical context is freed. You can save safely and of course everything is displayed in window.
  • If you need to save animation frames, just do this inside the draw at the end (should work, but your case shows that I can think wrong) or use additional canvas as a buffer, draw on it and save/display in a loop.

Could you share the code please?

Please don't look too closely, it's a WIP :) https://github.com/heyarne/line-us/blob/master/src/heyarne/line_us/doodles/seven.clj

Ok :) If you prefer, we can move to Slack (#clojure2d) or Zulip (I can create a stream for that). I have a couple of comments and can support you now.

saving in events: event processing is separated and not synchronized with drawing loop. I didn't implement the approach from Quil/Processing where event handler is called after drawing and releasing graphical context. Which assures that everything with drawing is done.

This was the issue. I'm going to close this, thanks!