-
-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Preet Shihn
authored and
Preet Shihn
committed
Jul 14, 2018
1 parent
938faec
commit a7c8314
Showing
38 changed files
with
2,259 additions
and
2,180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ResolvedOptions, Drawable } from './core'; | ||
import { RoughRenderer } from './renderer'; | ||
export declare abstract class RoughCanvasBase { | ||
protected canvas: HTMLCanvasElement; | ||
protected ctx: CanvasRenderingContext2D; | ||
constructor(canvas: HTMLCanvasElement); | ||
static createRenderer(): RoughRenderer; | ||
abstract getDefaultOptions(): ResolvedOptions; | ||
draw(drawable: Drawable): void; | ||
private computeBBox; | ||
private fillSketch; | ||
private _drawToContext; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { RoughRenderer } from './renderer'; | ||
const hasDocument = typeof document !== 'undefined'; | ||
export class RoughCanvasBase { | ||
constructor(canvas) { | ||
this.canvas = canvas; | ||
this.ctx = this.canvas.getContext('2d'); | ||
} | ||
static createRenderer() { | ||
return new RoughRenderer(); | ||
} | ||
draw(drawable) { | ||
const sets = drawable.sets || []; | ||
const o = drawable.options || this.getDefaultOptions(); | ||
const ctx = this.ctx; | ||
for (const drawing of sets) { | ||
switch (drawing.type) { | ||
case 'path': | ||
ctx.save(); | ||
ctx.strokeStyle = o.stroke; | ||
ctx.lineWidth = o.strokeWidth; | ||
this._drawToContext(ctx, drawing); | ||
ctx.restore(); | ||
break; | ||
case 'fillPath': | ||
ctx.save(); | ||
ctx.fillStyle = o.fill || ''; | ||
this._drawToContext(ctx, drawing); | ||
ctx.restore(); | ||
break; | ||
case 'fillSketch': | ||
this.fillSketch(ctx, drawing, o); | ||
break; | ||
case 'path2Dfill': { | ||
this.ctx.save(); | ||
this.ctx.fillStyle = o.fill || ''; | ||
const p2d = new Path2D(drawing.path); | ||
this.ctx.fill(p2d); | ||
this.ctx.restore(); | ||
break; | ||
} | ||
case 'path2Dpattern': { | ||
const doc = this.canvas.ownerDocument || (hasDocument && document); | ||
if (doc) { | ||
const size = drawing.size; | ||
const hcanvas = doc.createElement('canvas'); | ||
const hcontext = hcanvas.getContext('2d'); | ||
const bbox = this.computeBBox(drawing.path); | ||
if (bbox && (bbox.width || bbox.height)) { | ||
hcanvas.width = this.canvas.width; | ||
hcanvas.height = this.canvas.height; | ||
hcontext.translate(bbox.x || 0, bbox.y || 0); | ||
} | ||
else { | ||
hcanvas.width = size[0]; | ||
hcanvas.height = size[1]; | ||
} | ||
this.fillSketch(hcontext, drawing, o); | ||
this.ctx.save(); | ||
this.ctx.fillStyle = this.ctx.createPattern(hcanvas, 'repeat'); | ||
const p2d = new Path2D(drawing.path); | ||
this.ctx.fill(p2d); | ||
this.ctx.restore(); | ||
} | ||
else { | ||
console.error('Cannot render path2Dpattern. No defs/document defined.'); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
computeBBox(d) { | ||
if (hasDocument) { | ||
try { | ||
const ns = 'http://www.w3.org/2000/svg'; | ||
const svg = document.createElementNS(ns, 'svg'); | ||
svg.setAttribute('width', '0'); | ||
svg.setAttribute('height', '0'); | ||
const pathNode = self.document.createElementNS(ns, 'path'); | ||
pathNode.setAttribute('d', d); | ||
svg.appendChild(pathNode); | ||
document.body.appendChild(svg); | ||
const bbox = pathNode.getBBox(); | ||
document.body.removeChild(svg); | ||
return bbox; | ||
} | ||
catch (err) { } | ||
} | ||
return null; | ||
} | ||
fillSketch(ctx, drawing, o) { | ||
let fweight = o.fillWeight; | ||
if (fweight < 0) { | ||
fweight = o.strokeWidth / 2; | ||
} | ||
ctx.save(); | ||
ctx.strokeStyle = o.fill || ''; | ||
ctx.lineWidth = fweight; | ||
this._drawToContext(ctx, drawing); | ||
ctx.restore(); | ||
} | ||
_drawToContext(ctx, drawing) { | ||
ctx.beginPath(); | ||
for (const item of drawing.ops) { | ||
const data = item.data; | ||
switch (item.op) { | ||
case 'move': | ||
ctx.moveTo(data[0], data[1]); | ||
break; | ||
case 'bcurveTo': | ||
ctx.bezierCurveTo(data[0], data[1], data[2], data[3], data[4], data[5]); | ||
break; | ||
case 'qcurveTo': | ||
ctx.quadraticCurveTo(data[0], data[1], data[2], data[3]); | ||
break; | ||
case 'lineTo': | ||
ctx.lineTo(data[0], data[1]); | ||
break; | ||
} | ||
} | ||
if (drawing.type === 'fillPath') { | ||
ctx.fill(); | ||
} | ||
else { | ||
ctx.stroke(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.