Skip to content

Commit b2226de

Browse files
committed
add arrow shorcut to move selected object with keyboard
1 parent 97c5a5d commit b2226de

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/PaperCanvas.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class PaperCanvas extends React.Component {
3838

3939
this.exportJSON = this.exportJSON.bind(this);
4040
this.handleKeyPress = this.handleKeyPress.bind(this);
41+
this.handleKeyUp = this.handleKeyUp.bind(this);
4142

4243
this.testPaper1 = this.testPaper1.bind(this);
4344
this.testPaper2 = this.testPaper2.bind(this);
@@ -223,6 +224,7 @@ class PaperCanvas extends React.Component {
223224
this.paper.view.on('mousemove', this.mouseMove);
224225
this.paper.view.on('mouseup', this.mouseUp);
225226
this.paper.view.on('mousedown', this.mouseDown);
227+
this.paper.view.on('keyup', this.handleKeyUp)
226228
}
227229
initFrame() {
228230
let bounds = new this.paper.Path.Rectangle(0, 0, this.context.Params.Paper.width, this.context.Params.Paper.height);
@@ -573,6 +575,35 @@ class PaperCanvas extends React.Component {
573575
console.log(`Key "${event.key}" pressed [event: keydown]`)
574576
}
575577

578+
handleKeyUp(event)
579+
{
580+
console.log(`Key "${event.key}" pressed [event: keyup]`)
581+
if (this.selected) {
582+
if (event.key === "up")
583+
{
584+
this.selected.position.y = this.selected.position.y - 1;
585+
this.signalSelectedChange();
586+
}
587+
else if (event.key === "down")
588+
{
589+
this.selected.position.y = this.selected.position.y + 1;
590+
this.signalSelectedChange();
591+
}
592+
else if (event.key === "right")
593+
{
594+
this.selected.position.x = this.selected.position.x + 1;
595+
this.signalSelectedChange();
596+
}
597+
else if (event.key === "left")
598+
{
599+
this.selected.position.x = this.selected.position.x - 1;
600+
this.signalSelectedChange();
601+
}
602+
}
603+
604+
605+
}
606+
576607
exportJSON() {
577608
this.paper.activate();
578609
this.deselectAll();
@@ -837,11 +868,7 @@ class PaperCanvas extends React.Component {
837868

838869
let canvasWidth = this.canvasRef.current.offsetWidth /*/ window.devicePixelRatio*/;
839870
let canvasHeight = this.canvasRef.current.offsetHeight /*/ window.devicePixelRatio*/;
840-
//let xratio = canvasWidth / this.context.Params.Paper.width;
841-
//let yratio = canvasHeight / this.context.Params.Paper.height;
842-
//let pixelMillimeterRatio = Math.min(xratio, yratio);
843-
//this.canvasRef.current.width = this.canvasRef.current.offsetWidth;
844-
//this.canvasRef.current.height = this.canvasRef.current.offsetHeight;
871+
845872
this.paper.view.viewSize = [canvasWidth, canvasHeight];
846873
}
847874
testPaper2() {
@@ -944,7 +971,7 @@ class PaperCanvas extends React.Component {
944971

945972
return (
946973
<>
947-
<div id="falsediv" ref={this.divref} >
974+
<div id="falsediv" ref={this.divref} onKeyUp={this.handleKeyUp}>
948975
<canvas id={this.props.Id} ref={this.canvasRef} onKeyDown={this.handleKeyPress} resize hdpi>
949976
{this.props.children}
950977
</canvas>

0 commit comments

Comments
 (0)