|
| 1 | +def setup(): |
| 2 | + size(600, 600) |
| 3 | + background('#004477') |
| 4 | + ernest = createFont('Ernest.ttf', 20) |
| 5 | + textFont(ernest) |
| 6 | + noLoop() |
| 7 | + cursor(CROSS) |
| 8 | + |
| 9 | + |
| 10 | +swatches = ['#FF0000', '#FF9900', '#FFFF00', '#00FF00', '#0099FF', '#6633FF'] |
| 11 | +brushcolor = swatches[2] |
| 12 | +brushshape = ROUND |
| 13 | +brushsize = 3 |
| 14 | +painting = False |
| 15 | +paintmode = 'free' |
| 16 | +palette = 60 |
| 17 | + |
| 18 | + |
| 19 | +def draw(): |
| 20 | + print(frameCount) |
| 21 | + global painting, paintmode |
| 22 | + |
| 23 | + if mouseX < palette: |
| 24 | + paintmode = 'select' |
| 25 | + |
| 26 | + if paintmode == 'free': |
| 27 | + |
| 28 | + if painting: |
| 29 | + stroke(brushcolor) |
| 30 | + strokeCap(brushshape) |
| 31 | + strokeWeight(brushsize) |
| 32 | + line(mouseX, mouseY, pmouseX, pmouseY) |
| 33 | + |
| 34 | + elif frameCount > 1: |
| 35 | + painting = True |
| 36 | + |
| 37 | + # black panel |
| 38 | + noStroke() |
| 39 | + fill('#000000'); rect(0, 0, palette, height) |
| 40 | + # color swatches |
| 41 | + p, p2 = palette, palette/2 |
| 42 | + fill(swatches[0]); square(0, 0, p2) |
| 43 | + fill(swatches[1]); square(p2, 0, p2) |
| 44 | + fill(swatches[2]); square(0, p2, p2) |
| 45 | + fill(swatches[3]); square(p2, p2, p2) |
| 46 | + fill(swatches[4]); square(0, p, p2) |
| 47 | + fill(swatches[5]); square(p2, p, p2) |
| 48 | + # brush preview |
| 49 | + fill(brushcolor) |
| 50 | + if brushshape == ROUND: |
| 51 | + circle(30, 123, brushsize) |
| 52 | + paintmode = 'free' |
| 53 | + # clear button |
| 54 | + fill('#FFFFFF') |
| 55 | + text('CLEAR', 10, height-12) |
| 56 | + |
| 57 | + |
| 58 | +def mousePressed(): |
| 59 | + # start painting |
| 60 | + if mouseButton == LEFT: |
| 61 | + loop() |
| 62 | + # swatch select |
| 63 | + if mouseButton == LEFT and mouseX < palette and mouseY < 90: |
| 64 | + global brushcolor |
| 65 | + brushcolor = get(mouseX, mouseY) |
| 66 | + |
| 67 | + |
| 68 | +def mouseReleased(): |
| 69 | + # stop painting |
| 70 | + if mouseButton == LEFT: |
| 71 | + global painting |
| 72 | + painting = False |
| 73 | + noLoop() |
| 74 | + |
| 75 | + |
| 76 | +def mouseWheel(e): |
| 77 | + # resize the brush |
| 78 | + global brushsize, paintmode |
| 79 | + paintmode = 'select' |
| 80 | + brushsize += e.count |
| 81 | + if brushsize < 3: |
| 82 | + brushsize = 3 |
| 83 | + if brushsize > 45: |
| 84 | + brushsize = 45 |
| 85 | + redraw() |
| 86 | + |
| 87 | + |
| 88 | +def keyPressed(): |
| 89 | + global brushcolor, paintmode |
| 90 | + paintmode = 'select' |
| 91 | + # color swatch shortcuts |
| 92 | + if key.isdigit(): |
| 93 | + k = int(key) - 1 |
| 94 | + if k < len(swatches): |
| 95 | + brushcolor = swatches[k] |
| 96 | + redraw() |
| 97 | + |
| 98 | + |
| 99 | +def mouseClicked(): |
| 100 | + # clear canvas |
| 101 | + if mouseButton == LEFT and mouseX < 60 and mouseY > height-30: |
| 102 | + fill('#004477') |
| 103 | + rect(palette, 0, width, height) |
0 commit comments