blend-mode

create-graphics

blend-mode

Arguments
[mode]
Docstring

Blends the pixels in the display window according to the defined mode. There is a choice of the following modes to blend the source pixels (A) with the ones of pixels already in the display window (B):

  • :blend - linear interpolation of colours: C = A*factor + B
  • :add - additive blending with white clip: C = min(A*factor + B, 255)
  • :subtract - subtractive blending with black clip: C = max(B - A*factor, 0)
  • :darkest - only the darkest colour succeeds: C = min(A*factor, B)
  • :lightest - only the lightest colour succeeds: C = max(A*factor, B)
  • :difference - subtract colors from underlying image.
  • :exclusion - similar to :difference, but less extreme.
  • :multiply - Multiply the colors, result will always be darker.
  • :screen - Opposite multiply, uses inverse values of the colors.
  • :replace - the pixels entirely replace the others and don't utilize alpha (transparency) values.
  • :overlay - mix of :multiply and :screen. Multiplies dark values, and screens light values.
  • :hard-light - :screen when greater than 50% gray, :multiply when lower.
  • :soft-light - mix of :darkest and :lightest. Works like :overlay, but not as harsh.
  • :dodge - lightens light tones and increases contrast, ignores darks.
  • :burn - darker areas are applied, increasing contrast, ignores lights.

Note: in clj :hard-light, :soft-light, :overlay, :dodge, :burn modes are not supported. In cljs :subtract mode is not supported.

factor is the alpha value of the pixel being drawn

Works only inside sketch functions?
Yes
Original Processing method
blendMode()
Original p5js method
blendMode()
Example
clojure specific
  • (q/background 255)
    (let [modes [:replace :blend :add :subtract :darkest :lightest :difference
                 :exclusion :multiply :screen]
          splitted (partition-all 4 modes)]
      (dotimes [row (count splitted)]
        (dotimes [col (count (nth splitted row))]
          (let [mode (nth (nth splitted row) col)
                gr (q/create-graphics 100 100 :p2d)]
            (q/with-graphics gr
              (q/background 127)
              (q/blend-mode mode)
              (q/no-stroke)
              (q/fill 255 0 0)
              (q/rect 10 20 80 20)
              (q/fill 50 170 255 127)
              (q/rect 60 10 20 80)
              (q/fill 200 130 150 200)
              (q/rect 10 60 80 20)
              (q/fill 20 240 50 50)
              (q/rect 20 10 20 80))
            (q/image gr (* col 120) (* row 120))))))
clojurescript specific
  • (q/background 255)
    (let [modes [:replace :blend :add :darkest :lightest :difference :exclusion
                 :multiply :screen :overlay :hard-light :soft-light :dodge :burn]
          splitted (partition-all 4 modes)]
      (dotimes [row (count splitted)]
        (dotimes [col (count (nth splitted row))]
          (let [mode (nth (nth splitted row) col)
                gr (q/create-graphics 100 100 :p2d)]
            (q/with-graphics gr
              (q/background 127)
              (q/blend-mode mode)
              (q/no-stroke)
              (q/fill 255 0 0)
              (q/rect 10 20 80 20)
              (q/fill 50 170 255 127)
              (q/rect 60 10 20 80)
              (q/fill 200 130 150 200)
              (q/rect 10 60 80 20)
              (q/fill 20 240 50 50)
              (q/rect 20 10 20 80))
            (q/image gr (* col 120) (* row 120))))))
    try example

create-graphics

Arguments
[w h renderer path]clj[w h][w h renderer]
Docstring

Creates and returns a new PGraphics object of the types :p2d, :p3d, :java2d, :pdf. By default :java2d is used. Use this class if you need to draw into an off-screen graphics buffer. It's not possible to use create-graphics with the :opengl renderer, because it doesn't allow offscreen use. The :pdf renderer requires the filename parameter.

Note: don't use create-graphics in draw in Clojurescript, it leaks memory. You should create graphic in setup and reuse it in draw instead of creating a new one.

It's important to call any drawing commands between (.beginDraw graphics) and (.endDraw graphics) statements or use with-graphics macro. This is also true for any commands that affect drawing, such as smooth or color-mode.

If you're using :pdf renderer - don't forget to call (.dispose graphics) as last command inside with-graphics macro, otherwise graphics won't be saved.

Unlike the main drawing surface which is completely opaque, surfaces created with create-graphics can have transparency. This makes it possible to draw into a graphics and maintain the alpha channel. By using save to write a PNG or TGA file, the transparency of the graphics object will be honored.

Works only inside sketch functions?
Yes
Original Processing method
createGraphics()
Original p5js method
createGraphics()
Example
clojure specific
  • (q/background 255)
    (let [gr (q/create-graphics 100 100)]
      (q/with-graphics gr
        (q/background 127)
        (q/ellipse 50 50 80 40))
      (q/image gr 0 0))
    ; use different renderer
    (let [gr (q/create-graphics 100 100 :java2d)]
      (q/with-graphics gr
        (q/background 127)
        (q/ellipse 50 50 40 80))
      (q/image gr 100 100))
    ; write output to pdf file
    (let [gr (q/create-graphics 100 100 :pdf "generated/create-graphics.pdf")]
      (q/with-graphics gr
        (q/background 127)
        (q/ellipse 50 50 80 40)
        (.dispose gr)))
clojurescript specific
  • (q/background 255)
    (let [gr (q/create-graphics 100 100)]
      (q/with-graphics gr
        (q/background 127)
        (q/ellipse 50 50 80 40))
      (q/image gr 0 0))
    try example