|
| 1 | +import { RoughGenerator } from './generator'; |
| 2 | +import { RoughRenderer } from './renderer'; |
| 3 | +const hasDocument = typeof document !== 'undefined'; |
| 4 | +export class RoughCanvas { |
| 5 | + constructor(canvas, config) { |
| 6 | + this.canvas = canvas; |
| 7 | + this.ctx = this.canvas.getContext('2d'); |
| 8 | + this.gen = new RoughGenerator(config || null, this.canvas); |
| 9 | + } |
| 10 | + get generator() { |
| 11 | + return this.gen; |
| 12 | + } |
| 13 | + static createRenderer() { |
| 14 | + return new RoughRenderer(); |
| 15 | + } |
| 16 | + line(x1, y1, x2, y2, options) { |
| 17 | + const d = this.gen.line(x1, y1, x2, y2, options); |
| 18 | + this.draw(d); |
| 19 | + return d; |
| 20 | + } |
| 21 | + rectangle(x, y, width, height, options) { |
| 22 | + const d = this.gen.rectangle(x, y, width, height, options); |
| 23 | + this.draw(d); |
| 24 | + return d; |
| 25 | + } |
| 26 | + ellipse(x, y, width, height, options) { |
| 27 | + const d = this.gen.ellipse(x, y, width, height, options); |
| 28 | + this.draw(d); |
| 29 | + return d; |
| 30 | + } |
| 31 | + circle(x, y, diameter, options) { |
| 32 | + const d = this.gen.circle(x, y, diameter, options); |
| 33 | + this.draw(d); |
| 34 | + return d; |
| 35 | + } |
| 36 | + linearPath(points, options) { |
| 37 | + const d = this.gen.linearPath(points, options); |
| 38 | + this.draw(d); |
| 39 | + return d; |
| 40 | + } |
| 41 | + polygon(points, options) { |
| 42 | + const d = this.gen.polygon(points, options); |
| 43 | + this.draw(d); |
| 44 | + return d; |
| 45 | + } |
| 46 | + arc(x, y, width, height, start, stop, closed = false, options) { |
| 47 | + const d = this.gen.arc(x, y, width, height, start, stop, closed, options); |
| 48 | + this.draw(d); |
| 49 | + return d; |
| 50 | + } |
| 51 | + curve(points, options) { |
| 52 | + const d = this.gen.curve(points, options); |
| 53 | + this.draw(d); |
| 54 | + return d; |
| 55 | + } |
| 56 | + path(d, options) { |
| 57 | + const drawing = this.gen.path(d, options); |
| 58 | + this.draw(drawing); |
| 59 | + return drawing; |
| 60 | + } |
| 61 | + draw(drawable) { |
| 62 | + const sets = drawable.sets || []; |
| 63 | + const o = drawable.options || this.gen.defaultOptions; |
| 64 | + const ctx = this.ctx; |
| 65 | + for (const drawing of sets) { |
| 66 | + switch (drawing.type) { |
| 67 | + case 'path': |
| 68 | + ctx.save(); |
| 69 | + ctx.strokeStyle = o.stroke; |
| 70 | + ctx.lineWidth = o.strokeWidth; |
| 71 | + this._drawToContext(ctx, drawing); |
| 72 | + ctx.restore(); |
| 73 | + break; |
| 74 | + case 'fillPath': |
| 75 | + ctx.save(); |
| 76 | + ctx.fillStyle = o.fill || ''; |
| 77 | + this._drawToContext(ctx, drawing); |
| 78 | + ctx.restore(); |
| 79 | + break; |
| 80 | + case 'fillSketch': |
| 81 | + this.fillSketch(ctx, drawing, o); |
| 82 | + break; |
| 83 | + case 'path2Dfill': { |
| 84 | + this.ctx.save(); |
| 85 | + this.ctx.fillStyle = o.fill || ''; |
| 86 | + const p2d = new Path2D(drawing.path); |
| 87 | + this.ctx.fill(p2d); |
| 88 | + this.ctx.restore(); |
| 89 | + break; |
| 90 | + } |
| 91 | + case 'path2Dpattern': { |
| 92 | + if (hasDocument) { |
| 93 | + const size = drawing.size; |
| 94 | + const hcanvas = document.createElement('canvas'); |
| 95 | + const hcontext = hcanvas.getContext('2d'); |
| 96 | + const bbox = this.computeBBox(drawing.path); |
| 97 | + if (bbox && (bbox.width || bbox.height)) { |
| 98 | + hcanvas.width = this.canvas.width; |
| 99 | + hcanvas.height = this.canvas.height; |
| 100 | + hcontext.translate(bbox.x || 0, bbox.y || 0); |
| 101 | + } |
| 102 | + else { |
| 103 | + hcanvas.width = size[0]; |
| 104 | + hcanvas.height = size[1]; |
| 105 | + } |
| 106 | + this.fillSketch(hcontext, drawing, o); |
| 107 | + this.ctx.save(); |
| 108 | + this.ctx.fillStyle = this.ctx.createPattern(hcanvas, 'repeat'); |
| 109 | + const p2d = new Path2D(drawing.path); |
| 110 | + this.ctx.fill(p2d); |
| 111 | + this.ctx.restore(); |
| 112 | + } |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + computeBBox(d) { |
| 119 | + if (hasDocument) { |
| 120 | + try { |
| 121 | + const ns = 'http://www.w3.org/2000/svg'; |
| 122 | + const svg = document.createElementNS(ns, 'svg'); |
| 123 | + svg.setAttribute('width', '0'); |
| 124 | + svg.setAttribute('height', '0'); |
| 125 | + const pathNode = self.document.createElementNS(ns, 'path'); |
| 126 | + pathNode.setAttribute('d', d); |
| 127 | + svg.appendChild(pathNode); |
| 128 | + document.body.appendChild(svg); |
| 129 | + const bbox = pathNode.getBBox(); |
| 130 | + document.body.removeChild(svg); |
| 131 | + return bbox; |
| 132 | + } |
| 133 | + catch (err) { } |
| 134 | + } |
| 135 | + return null; |
| 136 | + } |
| 137 | + fillSketch(ctx, drawing, o) { |
| 138 | + let fweight = o.fillWeight; |
| 139 | + if (fweight < 0) { |
| 140 | + fweight = o.strokeWidth / 2; |
| 141 | + } |
| 142 | + ctx.save(); |
| 143 | + ctx.strokeStyle = o.fill || ''; |
| 144 | + ctx.lineWidth = fweight; |
| 145 | + this._drawToContext(ctx, drawing); |
| 146 | + ctx.restore(); |
| 147 | + } |
| 148 | + _drawToContext(ctx, drawing) { |
| 149 | + ctx.beginPath(); |
| 150 | + for (const item of drawing.ops) { |
| 151 | + const data = item.data; |
| 152 | + switch (item.op) { |
| 153 | + case 'move': |
| 154 | + ctx.moveTo(data[0], data[1]); |
| 155 | + break; |
| 156 | + case 'bcurveTo': |
| 157 | + ctx.bezierCurveTo(data[0], data[1], data[2], data[3], data[4], data[5]); |
| 158 | + break; |
| 159 | + case 'qcurveTo': |
| 160 | + ctx.quadraticCurveTo(data[0], data[1], data[2], data[3]); |
| 161 | + break; |
| 162 | + case 'lineTo': |
| 163 | + ctx.lineTo(data[0], data[1]); |
| 164 | + break; |
| 165 | + } |
| 166 | + } |
| 167 | + if (drawing.type === 'fillPath') { |
| 168 | + ctx.fill(); |
| 169 | + } |
| 170 | + else { |
| 171 | + ctx.stroke(); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments