| 1 | import { drawTrunk, fillCircle, fillEllipse, setPixel } from "../common.mjs"; |
| 2 | |
| 3 | const BARK = Object.freeze([68, 44, 28, 255]); |
| 4 | const BARK_SHADOW = Object.freeze([48, 30, 20, 255]); |
| 5 | |
| 6 | function mysticGlow (foliage) |
| 7 | { |
| 8 | return [ |
| 9 | Math.min(255, foliage[0] + 36), |
| 10 | Math.min(255, foliage[1] + 48), |
| 11 | Math.min(255, foliage[2] + 28), |
| 12 | 255 |
| 13 | ]; |
| 14 | } |
| 15 | |
| 16 | function drawLine (image, x0, y0, x1, y1, rgba) |
| 17 | { |
| 18 | const steps = Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0), 1); |
| 19 | for (let i = 0; i <= steps; i++) |
| 20 | { |
| 21 | const t = i / steps; |
| 22 | setPixel(image, Math.round(x0 + (x1 - x0) * t), Math.round(y0 + (y1 - y0) * t), rgba); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | function drawGnarledBranch (image, points, rgba = BARK) |
| 27 | { |
| 28 | for (let i = 0; i < points.length - 1; i++) |
| 29 | { |
| 30 | const [x0, y0] = points[i]; |
| 31 | const [x1, y1] = points[i + 1]; |
| 32 | drawLine(image, x0, y0, x1, y1, rgba); |
| 33 | if (i > 0 && i < points.length - 1) |
| 34 | { setPixel(image, x0, y0, BARK_SHADOW); } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | function leafCluster (image, x, y, foliage, radius = 2) |
| 39 | { |
| 40 | fillCircle(image, x, y, radius, foliage); |
| 41 | fillCircle(image, x, y, Math.max(1, radius - 1), mysticGlow(foliage)); |
| 42 | } |
| 43 | |
| 44 | /** Elder — gnarled sparse branches, small clusters, mystical ancient silhouette. */ |
| 45 | export function drawElderCanopy (image, season, stump, foliage) |
| 46 | { |
| 47 | drawTrunk(image, 16, 34, 44, 3); |
| 48 | drawTrunk(image, 15, 28, 35, 2); |
| 49 | setPixel(image, 17, 30, BARK_SHADOW); |
| 50 | setPixel(image, 14, 33, BARK_SHADOW); |
| 51 | |
| 52 | drawGnarledBranch(image, [[16, 30], [14, 26], [11, 21], [9, 16], [7, 11]]); |
| 53 | drawGnarledBranch(image, [[16, 29], [18, 24], [21, 19], [23, 14], [25, 9]]); |
| 54 | drawGnarledBranch(image, [[15, 32], [10, 28], [7, 23], [5, 18], [4, 13]]); |
| 55 | drawGnarledBranch(image, [[17, 31], [22, 27], [26, 22], [28, 17], [29, 12]]); |
| 56 | drawGnarledBranch(image, [[16, 28], [16, 22], [15, 16], [16, 10], [16, 6]]); |
| 57 | |
| 58 | drawGnarledBranch(image, [[11, 21], [13, 19], [15, 17]], BARK_SHADOW); |
| 59 | drawGnarledBranch(image, [[21, 19], [19, 17], [17, 15]], BARK_SHADOW); |
| 60 | |
| 61 | leafCluster(image, 7, 11, foliage, 2); |
| 62 | leafCluster(image, 25, 9, foliage, 2); |
| 63 | leafCluster(image, 4, 13, foliage, 2); |
| 64 | leafCluster(image, 29, 12, foliage, 2); |
| 65 | leafCluster(image, 16, 6, foliage, 3); |
| 66 | leafCluster(image, 15, 17, foliage, 2); |
| 67 | leafCluster(image, 17, 15, foliage, 2); |
| 68 | leafCluster(image, 9, 16, foliage, 2); |
| 69 | |
| 70 | fillEllipse(image, 16, 4, 2, 1, mysticGlow(foliage)); |
| 71 | } |