Created
August 26, 2020 19:36
-
-
Save yswallow/9fbf1001ce107e862b42d572a339bf4e to your computer and use it in GitHub Desktop.
長方形 グリグリやって 遊ぶやつ (575)
This file contains hidden or 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
| <html> | |
| <body> | |
| <canvas id="drawing" width="480" height="270" ></canvas> | |
| <li id="areas"></li> | |
| <script> | |
| const CanvasArea = { | |
| elements: { | |
| canvas: document.getElementById("drawing"), | |
| }, | |
| zoomX: 480/320, | |
| zoomY: 270/240, | |
| ctx: null, | |
| tempRect: { | |
| x1: 0, | |
| x2: 0, | |
| y1: 0, | |
| y2: 0, | |
| visible: false, | |
| draw: function(color){ | |
| CanvasArea.drawRect(this.x1,this.y1,this.x2,this.y2,color); | |
| }, | |
| set: function(x1,y1,x2,y2) { | |
| this.x1 = x1; | |
| this.y1 = y1; | |
| this.x2 = x2; | |
| this.y2 = y2; | |
| this.visible = true; | |
| } | |
| }, | |
| getPosition: function(e) { | |
| rect = e.target.getBoundingClientRect(); | |
| const position = {}; | |
| position.absoluteX = e.clientX - rect.left; | |
| position.absoluteY = e.clientY - rect.top; | |
| position.x = parseInt( position.absoluteX / this.zoomX); | |
| position.y = parseInt( position.absoluteY / this.zoomY); | |
| return position; | |
| }, | |
| clear: function() { | |
| this.ctx.fillStyle = "white"; | |
| this.ctx.fillRect(0,0,480,270); | |
| }, | |
| drawRect: function(x1,y1,x2,y2,color) { | |
| this.ctx.fillStyle = "black"; | |
| this.ctx.fillRect( | |
| this.zoomX*x1, | |
| this.zoomY*y1, | |
| this.zoomX*(x2-x1), | |
| this.zoomY*(y2-y1) | |
| ); | |
| this.ctx.fillStyle = color; | |
| this.ctx.fillRect( | |
| this.zoomX*(x1+2), | |
| this.zoomY*(y1+2), | |
| this.zoomX*(x2-x1-4), | |
| this.zoomY*(y2-y1-4) | |
| ); | |
| }, | |
| }; | |
| CanvasArea.ctx = CanvasArea.elements.canvas.getContext("2d"); | |
| CanvasArea.elements.canvas.addEventListener("pointerdown", (e)=>{ | |
| AreaConfigs.getActiveArea().pointerdown(e); | |
| }); | |
| CanvasArea.elements.canvas.addEventListener("pointermove", (e)=>{ | |
| AreaConfigs.getActiveArea().pointermove(e); | |
| }); | |
| CanvasArea.elements.canvas.addEventListener("pointerup", (e)=>{ | |
| AreaConfigs.getActiveArea().pointerup(e); | |
| }); | |
| CanvasArea.elements.canvas.addEventListener("pointerleave", (e)=>{ | |
| AreaConfigs.getActiveArea().pointerup(e); | |
| }) ; | |
| function createAreaConfig() { | |
| const areaConfig = { | |
| x1: 100, | |
| y1: 100, | |
| x2: 200, | |
| y2: 200, | |
| canvasState: 0, | |
| startPosition: null, | |
| getStateX: function() { | |
| return this.canvasState&7; | |
| }, | |
| getStateY: function() { | |
| return (this.canvasState>>4)&7; | |
| }, | |
| getDiff: function(position) { | |
| diff = {x:0, y:0}; | |
| if(this.startPosition) { | |
| diff.x = position.x - this.startPosition.x; | |
| diff.y = position.y - this.startPosition.y; | |
| } | |
| return diff; | |
| }, | |
| cursorPlace: function(position) { | |
| /* | b010 | | |
| * b001 | | b100 | |
| * 1| 3| 2 |6 |4 | |
| * || || | || || | |
| * 16-> v| v| v |v |v | |
| * ------ --+--+--------+--+---- | |
| * b001 48->|51| 50 |54| | |
| * ----------+--+--------+--+---- | |
| * | | | | | |
| * b010 32->|35| 34 |38| | |
| * | | | | | |
| * ----------+--+--------+--+---- | |
| * b100 96->|99| 98 |102| | |
| * ------ --+--+--------+--+---- | |
| * (<<4) 64->| | | | | |
| * | | | | | |
| */ | |
| bit = 0; | |
| width = 4; | |
| if(position.x < this.x1+width) { | |
| bit += 1; | |
| } else if( this.x2-width < position.x ) { | |
| bit += 4; | |
| } | |
| if(this.x1-width < position.x && position.x < this.x2+width) { | |
| bit += 2; | |
| } | |
| if( position.y < this.y1+width) { | |
| bit += 16; | |
| } else if(this.y2-width < position.y) { | |
| bit += 64; | |
| } | |
| if(this.y1-width < position.y && position.y < this.y2+width ) { | |
| bit += 32; | |
| } | |
| return bit; | |
| }, | |
| is_inner: function(position) { | |
| place = this.cursorPlace(position); | |
| return (place&2 && place&32); | |
| }, | |
| getCursorName: function(position) { | |
| i = this.cursorPlace(position); | |
| bitx = i % 16; | |
| bity = parseInt(i / 16); | |
| cursorName = "auto" | |
| if( bitx==3 || bitx==6) { | |
| if( bity==2 ) { | |
| cursorName = "ew-resize"; | |
| } | |
| } else if( bity==3 || bity==6 ) { | |
| if( bitx==2) { | |
| cursorName = "ns-resize"; | |
| } | |
| } | |
| if( i==102 || i==51 ){ | |
| cursorName = "nwse-resize"; | |
| } else if( i==99 || i==54) { | |
| cursorName = "nesw-resize"; | |
| } else if ( i==34 ) { | |
| cursorName = "grab"; | |
| } | |
| return cursorName; | |
| }, | |
| update: function(x1,y1,x2,y2) { | |
| this.x1 = x1<x2 ? x1 : x2; | |
| this.x2 = x1<x2 ? x2 : x1; | |
| this.y1 = y1<y2 ? y1 : y2; | |
| this.y2 = y1<y2 ? y2 : y1; | |
| }, | |
| moveTo: function(position) { | |
| if(this.startPosition) { | |
| diffX = position.x - this.startPosition.x; | |
| diffY = position.y - this.startPosition.y; | |
| this.update(this.x1+diffX, this.y1+diffY, this.x2+diffX, this.y2+diffY); | |
| } | |
| }, | |
| pointerdown: function(e) { | |
| position = CanvasArea.getPosition(e); | |
| place = AreaConfigs.getActiveArea().cursorPlace(position); | |
| if(place&2 && place&32) { | |
| CanvasArea.elements.canvas.style.cursor = this.getCursorName(position); | |
| this.canvasState = this.cursorPlace(position); | |
| if( this.getStateX()==2 && this.getStateY()==2 ) { | |
| this.startPosition = position; | |
| CanvasArea.elements.canvas.style.cursor = "move"; | |
| } | |
| } | |
| }, | |
| pointerup: function(e) { | |
| if(this.canvasState){ | |
| position = CanvasArea.getPosition(e); | |
| if(this.getStateX()==3){ | |
| //x is over the left line | |
| if(this.getStateY()==3){ | |
| //x,y is upper left corner | |
| this.update(position.x, position.y, this.x2, this.y2); | |
| } else if(this.getStateY()==6) { | |
| //x,y is downer left corner | |
| this.update(position.x, this.y1, this.x2, position.y); | |
| } else if(this.getStateY()==2) { | |
| //x,y is over the left line | |
| this.update(position.x, this.y1, this.x2, this.y2); | |
| } else { | |
| // ??? | |
| } | |
| } else if(this.getStateX()==6) { | |
| // x is over the right line | |
| if(this.getStateY()==3){ | |
| //x,y is upper right corner | |
| this.update(this.x1, position.y, position.x, this.y2); | |
| } else if(this.getStateY()==6) { | |
| //x,y is downer right corner | |
| this.update(this.x1, this.y1, position.x, position.y); | |
| } else if(this.getStateY()==2) { | |
| //x,y is over the right line | |
| this.update(this.x1, this.y1, position.x, this.y2); | |
| } else { | |
| // ??? | |
| } | |
| } else if(this.getStateX()==2) { | |
| //x is between the lines | |
| if(this.getStateY()==3){ | |
| //x,y is over the upper line | |
| this.update(this.x1, position.y, this.x2, this.y2); | |
| } else if(this.getStateY()==6) { | |
| //x,y is over the downer line | |
| this.update(this.x1, this.y1, this.x2, position.y); | |
| } else if(this.getStateY()==2) { | |
| //x,y is inner the area | |
| this.moveTo(position); | |
| } else { | |
| // ??? | |
| } | |
| } else { | |
| } | |
| CanvasArea.clear(); | |
| this.draw("blue"); | |
| CanvasArea.elements.canvas.style.cursor = "auto"; | |
| if(this.getStateX()==2 && this.getStateY==2) { | |
| this.startPosition = null; | |
| } | |
| this.canvasState = 0; | |
| CanvasArea.tempRect.visible = false; | |
| } | |
| }, | |
| pointermove: function(e) { | |
| position = CanvasArea.getPosition(e); | |
| if(this.canvasState) { | |
| if(this.getStateX()==3){ | |
| //x is over the left line | |
| if(this.getStateY()==3){ | |
| //x,y is upper left corner | |
| CanvasArea.tempRect.set(position.x, position.y, this.x2, this.y2); | |
| } else if(this.getStateY()==6) { | |
| //x,y is downer left corner | |
| CanvasArea.tempRect.set(position.x, this.y1, this.x2, position.y); | |
| } else if(this.getStateY()==2) { | |
| //x,y is over the left line | |
| CanvasArea.tempRect.set(position.x, this.y1, this.x2, this.y2); | |
| } else { | |
| // ??? | |
| } | |
| } else if(this.getStateX()==6) { | |
| // x is over the right line | |
| if(this.getStateY()==3){ | |
| //x,y is upper right corner | |
| CanvasArea.tempRect.set(this.x1, position.y, position.x, this.y2); | |
| } else if(this.getStateY()==6) { | |
| //x,y is downer right corner | |
| CanvasArea.tempRect.set(this.x1, this.y1, position.x, position.y); | |
| } else if(this.getStateY()==2) { | |
| //x,y is over the right line | |
| CanvasArea.tempRect.set(this.x1, this.y1, position.x, this.y2); | |
| } else { | |
| // ??? | |
| } | |
| } else if(this.getStateX()==2) { | |
| //x is between the lines | |
| if(this.getStateY()==3){ | |
| //x,y is over the upper line | |
| CanvasArea.tempRect.set(this.x1, position.y, this.x2, this.y2); | |
| } else if(this.getStateY()==6) { | |
| //x,y is over the downer line | |
| CanvasArea.tempRect.set(this.x1, this.y1, this.x2, position.y); | |
| } else if(this.getStateY()==2) { | |
| //x,y is inner the area | |
| diff = this.getDiff(position); | |
| CanvasArea.tempRect.set( | |
| this.x1+diff.x, this.y1+diff.y, | |
| this.x2+diff.x, this.y2+diff.y | |
| ); | |
| } else { | |
| // ??? | |
| } | |
| } else { | |
| } | |
| CanvasArea.clear(); | |
| CanvasArea.tempRect.draw("tomato"); | |
| } else { | |
| CanvasArea.elements.canvas.style.cursor = this.getCursorName(position); | |
| } | |
| }, | |
| draw: function(color) { | |
| CanvasArea.drawRect(this.x1, this.y1, this.x2, this.y2, color); | |
| }, | |
| }; | |
| return areaConfig; | |
| }; | |
| const AreaConfigs = { | |
| configs: [createAreaConfig()], | |
| getActiveArea: function() { | |
| return this.configs[0]; | |
| }, | |
| } | |
| AreaConfigs.getActiveArea().draw("blue"); | |
| </script> | |
| <style> | |
| canvas#drawing { | |
| border: solid; | |
| } | |
| </style> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment