|
| 1 | +# createGraphics Offscreen |
| 2 | + |
| 3 | +Sometimes you may find a need for creating an offscreen canvas and drawing graphics to this hidden screen. For that we have the [createGraphics()](/reference/createGraphics) function. |
| 4 | + |
| 5 | +In this example, a type of drawing program, dragging with the mouse across the window will draw tiny photos wherever our mouse is located. If this was the entirety of the program, we'd have no need for createGraphics. A minimal implementation of this follows: |
| 6 | + |
| 7 | +```lua |
| 8 | +require("L5") |
| 9 | + |
| 10 | +function setup() |
| 11 | + -- initalize a global array of images |
| 12 | + img = {} |
| 13 | + |
| 14 | + -- load all of our images |
| 15 | + for i=1,9 do |
| 16 | + img[i] = loadImage("assets/"..i..".png") |
| 17 | + end |
| 18 | + |
| 19 | + describe("Draws random images at mouse position when dragged in the window.") |
| 20 | +end |
| 21 | + |
| 22 | +function mouseDragged() |
| 23 | + image(random(img),mouseX, mouseY, 50, 50) |
| 24 | +end |
| 25 | +``` |
| 26 | + |
| 27 | +This program is fine, but what happens if we want a new color background each time we press down to draw a line or lay down photos? If we added `background()` to the `mouseDragged()` function than we would cover all of our previous drawing. To prevent this problem, we use an offscreen canvas. |
| 28 | + |
| 29 | +In our `setup()` we load in photos to an array `img`. And we initialize our `offscreenCanvas` as a new graphics buffer for drawing with `createGraphics()`. |
| 30 | + |
| 31 | +As draw runs continously, it grabs the offscreen canvas. We'll come back to that. In the mouseDragged event, as long as we are dragging the mouse we start drawing to the offscreen buffer, leaving a randomly-selected image at the equivalent of the mouse X and Y positions, but in the offscreen canvas. As we press down on the mouse, we draw a random color on the entire visible fullscreen. Then in draw, we take the offscreen graphics canvas and draw it as an image, starting at the top left 0, 0 position, effectively applying this offscreen doodle of images with transparent background on top of the color background. This allows us to maintain the design of drawing photos on a colored canvas without overwriting the entire window's contents. |
| 32 | + |
| 33 | +To extend the program, try changing the images, creating buttons to select images to draw with, add a secret scratch-off image finder or turn it into a coloring book game. You could even have multiple offscreen canvases and apply them to the window based on rules or randomness. |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +```lua |
| 38 | +require("L5") |
| 39 | + |
| 40 | +function setup() |
| 41 | + fullscreen() |
| 42 | + windowTitle("Quick Pic Draw") |
| 43 | + img = {} |
| 44 | + for i = 1, 9 do |
| 45 | + img[i] = loadImage("assets/"..i..".png") |
| 46 | + end |
| 47 | + |
| 48 | + background(0) |
| 49 | + describe("Draw with pictures.") |
| 50 | + |
| 51 | + -- Create offscreen canvas |
| 52 | + offscreenCanvas = createGraphics(800, 600) |
| 53 | +end |
| 54 | + |
| 55 | +function draw() |
| 56 | + image(offscreenCanvas:getCanvas(), 0, 0) |
| 57 | +end |
| 58 | + |
| 59 | +function mouseDragged() |
| 60 | + offscreenCanvas:beginDraw() |
| 61 | + |
| 62 | + image(random(img),mouseX,mouseY,50,50) |
| 63 | + |
| 64 | + offscreenCanvas:endDraw() |
| 65 | +end |
| 66 | + |
| 67 | +function mousePressed() |
| 68 | + background(random(255),random(255),random(255)) |
| 69 | +end |
| 70 | +``` |
| 71 | + |
| 72 | +## Related References |
| 73 | + |
| 74 | +* [createGraphics()](/reference/createGraphics) |
| 75 | +* [for](/reference/for) |
| 76 | +* [fullscreen()](/reference/fullscreen) |
| 77 | +* [image()](/reference/image) |
| 78 | +* [mouseDragged()](/reference/mouseDragged) |
| 79 | +* [mousePressed()](/reference/mousePressed) |
| 80 | + |
| 81 | +## Related Examples |
| 82 | + |
| 83 | +* [10print variations](10print.md) - An implementation of the classic maze-drawing algorithm |
| 84 | +* [Doodle draw](doodle-draw.md) - Basic implementation of a doodle drawing program |
| 85 | +* [Copy Image Data](imported-media-copy-image-data.md) - Paint from an image file onto the canvas. |
| 86 | +* [Daily Rituals](daily-rituals.md) - One a day daily ritual generator |
0 commit comments