-
Notifications
You must be signed in to change notification settings - Fork 565
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
added drawing functionality to tactical maps #3363
Closed
Closed
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
98fd3a2
added drawing functionality to tactical maps
b5b8683
removed redundant check
f458c8f
linter
eaeca24
more linter
be6d103
l-i-n-t-e-r
2a18885
prettier format
8cdfbeb
Merge branch 'master' into tacmap-refactor
c102fdc
progress
07d60ca
progress
a4cc32f
spaghet
a2de250
guh
9dd32dc
hacky, fix later.
8ed8d6d
fix
87d1c5b
Merge branch 'master' into tacmap-refactor
28e9a67
flaticon
108acb6
Merge branch 'tacmap-refactor' of github.com:Cthulhu80/cmss13 into ta…
03951dc
more more
6d6fe45
fix
efeabb8
bug fix
13037ba
fix
5ef3526
prettier
3d22bbd
fix
362df62
LINTER
d87da9e
fix
cf1cc1c
...
bdfadb9
fix
e21b083
Merge branch 'master' into tacmap-refactor
4cf8abf
typo
5d8784f
var refactor
3781a37
less shit code
22dbf83
fix
9757437
too much python lol
396ce26
fox
13151cb
fix
dcd697c
fix
be1cef3
hacky svg implementation
f923e09
fix
945df1c
fix
8c59c07
Merge branch 'tacmap-refactor' of github.com:Cthulhu80/cmss13 into ta…
97c7616
fuck yeah
4b27583
svg
18f1080
prettier
f54cb6e
Merge branch 'master' into tacmap-refactor
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,240 @@ | ||
import { Component, createRef } from 'inferno'; | ||
|
||
// this file should probably not be in interfaces, should move it later. | ||
export class CanvasLayer extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.canvasRef = createRef(); | ||
|
||
// color selection | ||
// using this.state prevents unpredictable behavior | ||
this.state = { | ||
selection: this.props.selection, | ||
}; | ||
|
||
// needs to be of type png of jpg | ||
this.img = null; | ||
this.imageSrc = this.props.imageSrc; | ||
|
||
// stores the stacked lines | ||
this.lineStack = []; | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
|
||
// stores the individual line drawn | ||
this.currentLine = []; | ||
|
||
this.ctx = null; | ||
this.isPainting = false; | ||
this.lastX = null; | ||
this.lastY = null; | ||
} | ||
|
||
componentDidMount() { | ||
this.ctx = this.canvasRef.current.getContext('2d'); | ||
this.ctx.lineWidth = 4; | ||
this.ctx.lineCap = 'round'; | ||
|
||
this.img = new Image(); | ||
|
||
// hardcoded value for testing pngs | ||
// this.img.src = "https://cm-ss13.com/wiki/images/6/6f/LV624.png" | ||
|
||
this.img.src = this.imageSrc; | ||
|
||
this.drawCanvas(); | ||
|
||
this.canvasRef.current.addEventListener('mousedown', this.handleMouseDown); | ||
this.canvasRef.current.addEventListener('mousemove', this.handleMouseMove); | ||
this.canvasRef.current.addEventListener('mouseup', this.handleMouseUp); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.canvasRef.current.removeEventListener( | ||
'mousedown', | ||
this.handleMouseDown | ||
); | ||
this.canvasRef.current.removeEventListener( | ||
'mousemove', | ||
this.handleMouseMove | ||
); | ||
this.canvasRef.current.removeEventListener('mouseup', this.handleMouseUp); | ||
} | ||
|
||
handleMouseDown = (e) => { | ||
this.isPainting = true; | ||
|
||
const rect = this.canvasRef.current.getBoundingClientRect(); | ||
const x = e.clientX - rect.left; | ||
const y = e.clientY - rect.top; | ||
|
||
this.ctx.beginPath(); | ||
this.ctx.moveTo(this.lastX, this.lastY); | ||
this.lastX = x; | ||
this.lastY = y; | ||
}; | ||
|
||
handleMouseMove = (e) => { | ||
if (!this.isPainting || !this.state.selection) { | ||
return; | ||
} | ||
|
||
// defaults to black sometimes, it's a bug I think maybe. | ||
this.ctx.strokeStyle = this.state.selection; | ||
|
||
const rect = this.canvasRef.current.getBoundingClientRect(); | ||
const x = e.clientX - rect.left; | ||
const y = e.clientY - rect.top; | ||
|
||
if (this.lastX !== null && this.lastY !== null) { | ||
this.ctx.moveTo(this.lastX, this.lastY); | ||
this.ctx.lineTo(x, y); | ||
this.ctx.stroke(); | ||
this.currentLine.push([ | ||
this.lastX, | ||
this.lastY, | ||
x, | ||
y, | ||
this.state.selection, | ||
]); | ||
} | ||
|
||
this.lastX = x; | ||
this.lastY = y; | ||
}; | ||
|
||
handleMouseUp = () => { | ||
this.isPainting = false; | ||
this.lineStack.push([...this.currentLine]); | ||
this.currentLine = []; | ||
this.ctx.beginPath(); | ||
}; | ||
|
||
handleSelectionChange = () => { | ||
const { selection } = this.props; | ||
|
||
if (selection === 'clear') { | ||
this.ctx.clearRect( | ||
0, | ||
0, | ||
this.canvasRef.current.width, | ||
this.canvasRef.current.height | ||
); | ||
this.ctx.drawImage( | ||
this.img, | ||
0, | ||
0, | ||
this.canvasRef.current.width, | ||
this.canvasRef.current.height | ||
); | ||
|
||
this.lineStack = []; | ||
return; | ||
} | ||
|
||
if (selection === 'undo') { | ||
if (this.lineStack.length === 0) { | ||
return; | ||
} | ||
const line = this.lineStack[this.lineStack.length - 1]; | ||
|
||
// selects last color before line is yeeted, this is buggy sometimes. | ||
const prevColor = line[0][4]; | ||
this.lineStack.pop(); | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
|
||
this.ctx.clearRect( | ||
0, | ||
0, | ||
this.canvasRef.current.width, | ||
this.canvasRef.current.height | ||
); | ||
this.ctx.drawImage( | ||
this.img, | ||
0, | ||
0, | ||
this.canvasRef.current.width, | ||
this.canvasRef.current.height | ||
); | ||
this.ctx.globalCompositeOperation = 'source-over'; | ||
|
||
this.lineStack.forEach((currentLine) => { | ||
currentLine.forEach(([lastX, lastY, x, y, colorSelection]) => { | ||
this.ctx.strokeStyle = colorSelection; | ||
this.ctx.beginPath(); | ||
this.ctx.moveTo(lastX, lastY); | ||
this.ctx.lineTo(x, y); | ||
this.ctx.stroke(); | ||
}); | ||
}); | ||
|
||
this.setState({ selection: prevColor }); | ||
return; | ||
} | ||
|
||
if (selection === 'export') { | ||
const svgData = this.convertToSVG(); | ||
this.props.onImageExport(svgData); | ||
return; | ||
} | ||
|
||
this.setState({ selection: selection }); | ||
}; | ||
|
||
componentDidUpdate(prevProps) { | ||
if (prevProps.selection !== this.props.selection) { | ||
this.handleSelectionChange(); | ||
} | ||
} | ||
|
||
drawCanvas() { | ||
this.img.onload = () => { | ||
// this onload may or may not be causing problems. | ||
this.ctx.drawImage( | ||
this.img, | ||
0, | ||
0, | ||
this.canvasRef.current.width, | ||
this.canvasRef.current.height | ||
); | ||
}; | ||
} | ||
|
||
convertToSVG() { | ||
const svgNS = 'http://www.w3.org/2000/svg'; | ||
const svg = document.createElementNS(svgNS, 'svg'); | ||
svg.setAttributeNS(null, 'width', this.canvasRef.current.width); | ||
svg.setAttributeNS(null, 'height', this.canvasRef.current.height); | ||
|
||
const lines = this.lineStack.flat(); | ||
lines.forEach(([lastX, lastY, x, y, colorSelection]) => { | ||
const line = document.createElementNS(svgNS, 'line'); | ||
line.setAttributeNS(null, 'x1', lastX); | ||
line.setAttributeNS(null, 'y1', lastY); | ||
line.setAttributeNS(null, 'x2', x); | ||
line.setAttributeNS(null, 'y2', y); | ||
line.setAttributeNS(null, 'stroke', colorSelection); | ||
line.setAttributeNS(null, 'stroke-width', '4'); | ||
line.setAttributeNS(null, 'stroke-linecap', 'round'); | ||
svg.appendChild(line); | ||
}); | ||
|
||
const serializer = new XMLSerializer(); | ||
const serializedSvg = serializer.serializeToString(svg); | ||
const base64Svg = btoa(serializedSvg); | ||
const dataUrl = `data:image/svg+xml;base64,${base64Svg}`; | ||
|
||
return dataUrl; | ||
} | ||
|
||
render() { | ||
return ( | ||
<canvas | ||
ref={this.canvasRef} | ||
style={{ | ||
height: '100%', // causes discrepency between mouse and drawing line, fix later. | ||
width: '100%', | ||
}} | ||
width={650} | ||
height={590} | ||
/> | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could this cause a performance issue if say, suddenly, 30-40 marines all request the image at once? I can't remember if repeated calls will recache it.
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.
my initial commit lul, why ever did I delete this comment