Skip to content

Commit 80b1cec

Browse files
committed
Fix some overwritten conflicts
1 parent eeb4e68 commit 80b1cec

File tree

3 files changed

+71
-47
lines changed

3 files changed

+71
-47
lines changed

demo/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class Demo extends Component {
122122
<button
123123
onClick={() => {
124124
console.log(this.saveableCanvas.getDataURL());
125+
alert("DataURL written to console")
125126
}}
126127
>
127128
GetDataURL

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ import makePassiveEventOption from "./makePassiveEventOption";
1313
function midPointBtw(p1, p2) {
1414
return {
1515
x: p1.x + (p2.x - p1.x) / 2,
16-
y: p1.y + (p2.y - p1.y) / 2
16+
y: p1.y + (p2.y - p1.y) / 2,
1717
};
1818
}
1919

2020
const canvasStyle = {
2121
display: "block",
22-
position: "absolute"
22+
position: "absolute",
2323
};
2424

2525
const canvasTypes = ["grid", "drawing", "temp", "interface"];
2626

2727
const dimensionsPropTypes = PropTypes.oneOfType([
2828
PropTypes.number,
29-
PropTypes.string
29+
PropTypes.string,
3030
]);
3131

3232
const boundsProp = PropTypes.shape({
@@ -114,7 +114,7 @@ export default class CanvasDraw extends PureComponent {
114114
this.interactionSM = new DefaultState();
115115
this.coordSystem = new CoordinateSystem({
116116
scaleExtents: props.zoomExtents,
117-
documentSize: { width: props.canvasWidth, height: props.canvasHeight }
117+
documentSize: { width: props.canvasWidth, height: props.canvasHeight },
118118
});
119119
this.coordSystem.attachViewChangeListener(this.applyView.bind(this));
120120
}
@@ -132,7 +132,7 @@ export default class CanvasDraw extends PureComponent {
132132
};
133133

134134
eraseAll = () => {
135-
this.erasedLines.push([ ...this.lines ]);
135+
this.erasedLines.push([...this.lines]);
136136
this.clearExceptErasedLines();
137137
this.triggerOnChange();
138138
};
@@ -156,10 +156,15 @@ export default class CanvasDraw extends PureComponent {
156156
return JSON.stringify({
157157
lines: this.lines,
158158
width: this.props.canvasWidth,
159-
height: this.props.canvasHeight
159+
height: this.props.canvasHeight,
160160
});
161161
};
162162

163+
getDataURL = () => {
164+
//generates the image data url (JPG format) from the HTML5 canvas element
165+
return this.canvas.drawing.toDataURL();
166+
};
167+
163168
loadSaveData = (saveData, immediate = this.props.immediateLoading) => {
164169
if (typeof saveData !== "string") {
165170
throw new Error("saveData needs to be of type string!");
@@ -179,7 +184,7 @@ export default class CanvasDraw extends PureComponent {
179184
) {
180185
this.simulateDrawingLines({
181186
lines,
182-
immediate
187+
immediate,
183188
});
184189
} else {
185190
// we need to rescale the lines based on saved & current dimensions
@@ -188,15 +193,15 @@ export default class CanvasDraw extends PureComponent {
188193
const scaleAvg = (scaleX + scaleY) / 2;
189194

190195
this.simulateDrawingLines({
191-
lines: lines.map(line => ({
196+
lines: lines.map((line) => ({
192197
...line,
193-
points: line.points.map(p => ({
198+
points: line.points.map((p) => ({
194199
x: p.x * scaleX,
195-
y: p.y * scaleY
200+
y: p.y * scaleY,
196201
})),
197-
brushRadius: line.brushRadius * scaleAvg
202+
brushRadius: line.brushRadius * scaleAvg,
198203
})),
199-
immediate
204+
immediate,
200205
});
201206
}
202207
};
@@ -211,8 +216,8 @@ export default class CanvasDraw extends PureComponent {
211216
enabled: true,
212217
initialPoint: {
213218
x: window.innerWidth / 2,
214-
y: window.innerHeight / 2
215-
}
219+
y: window.innerHeight / 2,
220+
},
216221
});
217222
this.chainLength = this.props.lazyRadius * window.devicePixelRatio;
218223

@@ -248,11 +253,12 @@ export default class CanvasDraw extends PureComponent {
248253
// Attach our wheel event listener here instead of in the render so that we can specify a non-passive listener.
249254
// This is necessary to prevent the default event action on chrome.
250255
// https://github.com/facebook/react/issues/14856
251-
this.canvas.interface && this.canvas.interface.addEventListener(
252-
"wheel",
253-
this.handleWheel,
254-
makePassiveEventOption()
255-
);
256+
this.canvas.interface &&
257+
this.canvas.interface.addEventListener(
258+
"wheel",
259+
this.handleWheel,
260+
makePassiveEventOption()
261+
);
256262
}
257263

258264
componentDidUpdate(prevProps) {
@@ -279,7 +285,8 @@ export default class CanvasDraw extends PureComponent {
279285

280286
componentWillUnmount = () => {
281287
this.canvasObserver.unobserve(this.canvasContainer);
282-
this.canvas.interface && this.canvas.interface.removeEventListener("wheel", this.handleWheel);
288+
this.canvas.interface &&
289+
this.canvas.interface.removeEventListener("wheel", this.handleWheel);
283290
};
284291

285292
render() {
@@ -292,20 +299,20 @@ export default class CanvasDraw extends PureComponent {
292299
touchAction: "none",
293300
width: this.props.canvasWidth,
294301
height: this.props.canvasHeight,
295-
...this.props.style
302+
...this.props.style,
296303
}}
297-
ref={container => {
304+
ref={(container) => {
298305
if (container) {
299306
this.canvasContainer = container;
300307
}
301308
}}
302309
>
303-
{canvasTypes.map(({ name, zIndex }) => {
310+
{canvasTypes.map((name) => {
304311
const isInterface = name === "interface";
305312
return (
306313
<canvas
307314
key={name}
308-
ref={canvas => {
315+
ref={(canvas) => {
309316
if (canvas) {
310317
this.canvas[name] = canvas;
311318
this.ctx[name] = canvas.getContext("2d");
@@ -314,7 +321,7 @@ export default class CanvasDraw extends PureComponent {
314321
}
315322
}
316323
}}
317-
style={{ ...canvasStyle, zIndex }}
324+
style={{ ...canvasStyle }}
318325
onMouseDown={isInterface ? this.handleDrawStart : undefined}
319326
onMouseMove={isInterface ? this.handleDrawMove : undefined}
320327
onMouseUp={isInterface ? this.handleDrawEnd : undefined}
@@ -332,21 +339,21 @@ export default class CanvasDraw extends PureComponent {
332339

333340
///// Event Handlers
334341

335-
handleWheel = e => {
342+
handleWheel = (e) => {
336343
this.interactionSM = this.interactionSM.handleMouseWheel(e, this);
337344
};
338345

339-
handleDrawStart = e => {
346+
handleDrawStart = (e) => {
340347
this.interactionSM = this.interactionSM.handleDrawStart(e, this);
341348
this.mouseHasMoved = true;
342349
};
343350

344-
handleDrawMove = e => {
351+
handleDrawMove = (e) => {
345352
this.interactionSM = this.interactionSM.handleDrawMove(e, this);
346353
this.mouseHasMoved = true;
347354
};
348355

349-
handleDrawEnd = e => {
356+
handleDrawEnd = (e) => {
350357
this.interactionSM = this.interactionSM.handleDrawEnd(e, this);
351358
this.mouseHasMoved = true;
352359
};
@@ -356,11 +363,13 @@ export default class CanvasDraw extends PureComponent {
356363
return;
357364
}
358365

359-
canvasTypes.map(({ name }) => this.ctx[name]).forEach(ctx => {
360-
this.clearWindow(ctx);
361-
const m = this.coordSystem.transformMatrix;
362-
ctx.setTransform(m.a, m.b, m.c, m.d, m.e, m.f);
363-
});
366+
canvasTypes
367+
.map(({ name }) => this.ctx[name])
368+
.forEach((ctx) => {
369+
this.clearWindow(ctx);
370+
const m = this.coordSystem.transformMatrix;
371+
ctx.setTransform(m.a, m.b, m.c, m.d, m.e, m.f);
372+
});
364373

365374
if (!this.deferRedrawOnViewChange) {
366375
this.drawGrid(this.ctx.grid);
@@ -409,7 +418,9 @@ export default class CanvasDraw extends PureComponent {
409418
};
410419

411420
redrawImage = () => {
412-
this.image && this.image.complete && drawImage({ ctx: this.ctx.grid, img: this.image });
421+
this.image &&
422+
this.image.complete &&
423+
drawImage({ ctx: this.ctx.grid, img: this.image });
413424
};
414425

415426
simulateDrawingLines = ({ lines, immediate }) => {
@@ -418,7 +429,7 @@ export default class CanvasDraw extends PureComponent {
418429
let curTime = 0;
419430
let timeoutGap = immediate ? 0 : this.props.loadTimeOffset;
420431

421-
lines.forEach(line => {
432+
lines.forEach((line) => {
422433
const { points, brushColor, brushRadius } = line;
423434

424435
// Draw all at once if immediate flag is set, instead of using setTimeout
@@ -427,7 +438,7 @@ export default class CanvasDraw extends PureComponent {
427438
this.drawPoints({
428439
points,
429440
brushColor,
430-
brushRadius
441+
brushRadius,
431442
});
432443

433444
// Save line with the drawn points
@@ -443,7 +454,7 @@ export default class CanvasDraw extends PureComponent {
443454
this.drawPoints({
444455
points: points.slice(0, i + 1),
445456
brushColor,
446-
brushRadius
457+
brushRadius,
447458
});
448459
}, curTime);
449460
}
@@ -500,7 +511,7 @@ export default class CanvasDraw extends PureComponent {
500511
this.lines.push({
501512
points: [...this.points],
502513
brushColor: brushColor || this.props.brushColor,
503-
brushRadius: brushRadius || this.props.brushRadius
514+
brushRadius: brushRadius || this.props.brushRadius,
504515
});
505516

506517
// Reset points array
@@ -510,7 +521,10 @@ export default class CanvasDraw extends PureComponent {
510521
this.inClientSpace([this.ctx.drawing, this.ctx.temp], () => {
511522
this.ctx.drawing.drawImage(
512523
this.canvas.temp,
513-
0, 0, this.canvas.drawing.width, this.canvas.drawing.height
524+
0,
525+
0,
526+
this.canvas.drawing.width,
527+
this.canvas.drawing.height
514528
);
515529
});
516530

@@ -524,8 +538,10 @@ export default class CanvasDraw extends PureComponent {
524538
this.props.onChange && this.props.onChange(this);
525539
};
526540

527-
clearWindow = ctx => {
528-
this.inClientSpace([ctx], () => ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height));
541+
clearWindow = (ctx) => {
542+
this.inClientSpace([ctx], () =>
543+
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
544+
);
529545
};
530546

531547
clearExceptErasedLines = () => {
@@ -553,15 +569,22 @@ export default class CanvasDraw extends PureComponent {
553569
};
554570

555571
inClientSpace = (ctxs, action) => {
556-
ctxs.forEach(ctx => {
572+
ctxs.forEach((ctx) => {
557573
ctx.save();
558-
ctx.setTransform(IDENTITY.a, IDENTITY.b, IDENTITY.c, IDENTITY.d, IDENTITY.e, IDENTITY.f);
574+
ctx.setTransform(
575+
IDENTITY.a,
576+
IDENTITY.b,
577+
IDENTITY.c,
578+
IDENTITY.d,
579+
IDENTITY.e,
580+
IDENTITY.f
581+
);
559582
});
560583

561584
try {
562585
action();
563586
} finally {
564-
ctxs.forEach(ctx => ctx.restore());
587+
ctxs.forEach((ctx) => ctx.restore());
565588
}
566589
};
567590

@@ -581,7 +604,7 @@ export default class CanvasDraw extends PureComponent {
581604
this.image.src = this.props.imgSrc;
582605
};
583606

584-
drawGrid = ctx => {
607+
drawGrid = (ctx) => {
585608
if (this.props.hideGrid) return;
586609

587610
this.clearWindow(ctx);

0 commit comments

Comments
 (0)