-
Notifications
You must be signed in to change notification settings - Fork 1
fix: full size canvas plus eraser #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1fc54f1
f3f5580
df861f2
8fd3b24
a739c6d
e14b647
6d79ff2
36b01c9
1176259
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pwa: true |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,29 @@ | ||
| import React from "react"; | ||
| import { SketchPicker } from "react-color"; | ||
| import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
| import { faEraser, faCircle, faSquare, faPaintBrush, faDownload, faSync } from '@fortawesome/free-solid-svg-icons' | ||
|
|
||
| interface Point { | ||
| x: number; | ||
| y: number; | ||
| } | ||
|
|
||
| interface Color { | ||
| hex: string; | ||
| } | ||
|
|
||
| class CanvasComponent extends React.Component { | ||
| colorInput; | ||
|
|
||
| constructor(props) { | ||
| super(props); | ||
| this.colorInput = React.createRef(); | ||
| } | ||
|
|
||
| state = { | ||
| background: "#121212", | ||
| color: "#000000", | ||
| choice: "brush", | ||
| canvas: {} as HTMLCanvasElement, | ||
| }; | ||
|
|
||
| handleChangeComplete = (color) => { | ||
| this.setState({ background: color.hex }); | ||
| handleChangeComplete = (e) => { | ||
| this.setState({ color: e.target.value }); | ||
| }; | ||
|
|
||
| componentDidMount() { | ||
|
|
@@ -29,6 +34,11 @@ class CanvasComponent extends React.Component { | |
| const canvas: HTMLCanvasElement = document.getElementById( | ||
| "canvas" | ||
| ) as HTMLCanvasElement; | ||
|
|
||
| // Setting up width and height of canvas | ||
| canvas.width = window.innerWidth; | ||
| canvas.height = window.innerHeight; | ||
|
|
||
| const canvasWidth = canvas.width; | ||
| const canvasHeight = canvas.height; | ||
| const ctx:any = canvas.getContext("2d"); | ||
|
|
@@ -41,7 +51,7 @@ class CanvasComponent extends React.Component { | |
| this.setState({ canvas }); | ||
|
|
||
| ctx.beginPath(); | ||
| ctx.rect(0, 0, canvasHeight, canvasWidth); | ||
| ctx.rect(0, 0, canvasWidth, canvasHeight); | ||
| ctx.fillStyle = "#ffffff"; | ||
| ctx.fill(); | ||
|
|
||
|
|
@@ -112,7 +122,7 @@ class CanvasComponent extends React.Component { | |
| plvaly = e.clientY; | ||
| } | ||
| ctx.drawImage(canvasPic, 0, 0); | ||
| ctx.fillStyle = this.state.background; | ||
| ctx.fillStyle = this.state.color; | ||
| ctx.beginPath(); | ||
| ctx.rect( | ||
| lastPoint.x, | ||
|
|
@@ -142,7 +152,7 @@ class CanvasComponent extends React.Component { | |
| plvaly = e.clientY; | ||
| } | ||
| ctx.drawImage(canvasPic, 0, 0); | ||
| ctx.fillStyle = this.state.background; | ||
| ctx.fillStyle = this.state.color; | ||
| ctx.beginPath(); | ||
| ctx.arc( | ||
| lastPoint.x, | ||
|
|
@@ -169,7 +179,7 @@ class CanvasComponent extends React.Component { | |
| for (let i = 0; i < dist; i++) { | ||
| x = lastPoint.x + Math.sin(angle) * i - 0; | ||
| y = lastPoint.y + Math.cos(angle) * i - 0; | ||
| ctx.fillStyle = this.state.background; | ||
| ctx.fillStyle = this.state.color; | ||
| ctx.beginPath(); | ||
| ctx.rect(x, y, 5, 5); | ||
| ctx.fill(); | ||
|
|
@@ -192,33 +202,46 @@ class CanvasComponent extends React.Component { | |
| render() { | ||
| return ( | ||
| <React.Fragment> | ||
| <canvas id="canvas" ref="canvas" width={500} height={500} /> | ||
| <canvas id="canvas" ref="canvas"/> | ||
| <div className="toolset"> | ||
| <button | ||
| onClick={() => { | ||
| this.setState({ choice: "brush" }); | ||
| }} | ||
| > | ||
| Brush | ||
| <FontAwesomeIcon icon={faPaintBrush}/> | ||
| </button> | ||
|
|
||
| <button | ||
| onClick={() => { | ||
| this.colorInput.current.value="#ffffff"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't change this directly. You should only change the state. |
||
| this.setState({ choice: "brush" }); | ||
| this.setState({ color: "#ffffff" }); | ||
| }} | ||
| > | ||
| <FontAwesomeIcon icon={faEraser}/> | ||
| </button> | ||
|
|
||
|
|
||
| <button | ||
| onClick={() => { | ||
| this.setState({ choice: "rectangle" }); | ||
| }} | ||
| > | ||
| Rectangle | ||
| <FontAwesomeIcon icon={faSquare}/> | ||
| </button> | ||
|
|
||
| <button | ||
| onClick={() => { | ||
| this.setState({ choice: "circle" }); | ||
| }} | ||
| > | ||
| Circle | ||
| <FontAwesomeIcon icon={faCircle}/> | ||
| </button> | ||
|
|
||
| <button onClick={this.updateCanvas.bind(this)}>Clear</button> | ||
| <button onClick={this.updateCanvas.bind(this)}> | ||
| <FontAwesomeIcon icon={faSync}/> | ||
| </button> | ||
|
|
||
| <button | ||
| onClick={() => { | ||
|
|
@@ -228,12 +251,9 @@ class CanvasComponent extends React.Component { | |
| link.click(); | ||
| }} | ||
| > | ||
| Download | ||
| <FontAwesomeIcon icon={faDownload}/> | ||
| </button> | ||
| <SketchPicker | ||
| color={this.state.background} | ||
| onChangeComplete={this.handleChangeComplete} | ||
| /> | ||
| <input type="color" ref={this.colorInput} onChange={this.handleChangeComplete}/> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The handler name can be more specific than
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set |
||
| </div> | ||
| </React.Fragment> | ||
| ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Canvas should not be a state, it should be a ref.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically, whatever data you need to render the component (color, choice, isDrawStarted, etc.) should either be a state or prop. UI elements like canvas, websocket connection, etc are important but they are not exactly data that you want to display. They should be stored inside refs. They are also mutable which might cause undefined behaviour with state: state and props should always be immutable.