forked from mattdesl/canvas-sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas-react.js
47 lines (39 loc) · 1.04 KB
/
canvas-react.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Example how to integrate canvas-sketch into react component
* @author @cheepo2109
*/
import React, { useEffect } from "react";
import canvasSketch from "canvas-sketch";
const settings = {
dimensions: "a4",
pixelsPerInch: 300,
units: "in"
};
const sketch = ({}) => {
//Basic example from canvas-sketch repo
return ({ context, width, height }) => {
// Margin in inches
const margin = 1 / 4;
// Off-white background
context.fillStyle = "hsl(0, 0%, 98%)";
context.fillRect(0, 0, width, height);
// Gradient foreground
const fill = context.createLinearGradient(0, 0, width, height);
fill.addColorStop(0, "cyan");
fill.addColorStop(1, "orange");
// Fill rectangle
context.fillStyle = fill;
context.fillRect(margin, margin, width - margin * 2, height - margin * 2);
};
};
const Sketch = () => {
const ref = React.createRef();
useEffect(() => {
canvasSketch(sketch, {
...settings,
canvas: ref.current
});
}, [ref]);
return <canvas ref={ref} />;
};
export default Sketch;