forked from comozilla/parapara-canvas-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ayato Sakurai
authored and
Ayato Sakurai
committed
May 7, 2016
1 parent
328b324
commit dd36df9
Showing
8 changed files
with
190 additions
and
169 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
import eventPublisher from "./publisher"; | ||
|
||
// HTMLCanvasElementをラップし, canvasRenderingContext2Dに関する操作を提供する | ||
function CanvasModel(element) { | ||
this.element = element; | ||
this.context = this.element.getContext("2d"); | ||
} | ||
|
||
// 外からはアクセスしないでください。 | ||
CanvasModel.prototype.getImageData = function() { | ||
return this.context.getImageData(0, 0, | ||
this.element.width, this.element.height); | ||
}; | ||
|
||
CanvasModel.prototype.setImageData = function(imageData) { | ||
this.context.clearRect(0, 0, | ||
this.element.width, this.element.height); | ||
if (imageData !== null) { | ||
this.context.putImageData(imageData, 0, 0); | ||
class CanvasModel { | ||
constructor(element) { | ||
this.element = element; | ||
this.context = this.element.getContext("2d"); | ||
} | ||
}; | ||
getImageData() { | ||
return this.context.getImageData(0, 0, | ||
this.element.width, this.element.height); | ||
} | ||
setImageData(imageData) { | ||
this.context.clearRect(0, 0, | ||
this.element.width, this.element.height); | ||
if (imageData !== null) { | ||
this.context.putImageData(imageData, 0, 0); | ||
} | ||
} | ||
} | ||
|
||
export default CanvasModel; |
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 |
---|---|---|
@@ -1,33 +1,36 @@ | ||
import eventPublisher from "./publisher"; | ||
|
||
function DrawingConfiguration() { | ||
this.defaultPalleteColors = []; | ||
this.color = ""; | ||
this.lineWidth = 0; | ||
} | ||
class DrawingConfiguration { | ||
constructor() { | ||
this.defaultPalleteColors = []; | ||
this.color = ""; | ||
this.lineWidth = 0; | ||
} | ||
|
||
// これをコンストラクタに入れないのは、 | ||
// ViewManager(など)で、subscribeした後に、 | ||
// publishをしたいため。 | ||
setDefaultValues() { | ||
this.defaultPalleteColors = []; | ||
eventPublisher.subscribe("defaultPalleteColors:after", | ||
(defaultPalleteColors) => { | ||
this.defaultPalleteColors = defaultPalleteColors; | ||
}); | ||
eventPublisher.publish("defaultPalleteColors", ["red", "orange", "yellow", | ||
"lightgreen", "green", "skyblue", "blue", "purple", "black", "white"]); | ||
|
||
// これをコンストラクタに入れないのは、 | ||
// ViewManager(など)で、subscribeした後に、 | ||
// publishをしたいため。 | ||
DrawingConfiguration.prototype.setDefaultValues = function() { | ||
this.defaultPalleteColors = []; | ||
eventPublisher.subscribe("defaultPalleteColors:after", | ||
(defaultPalleteColors) => { | ||
this.defaultPalleteColors = defaultPalleteColors; | ||
}); | ||
eventPublisher.publish("defaultPalleteColors", ["red", "orange", "yellow", | ||
"lightgreen", "green", "skyblue", "blue", "purple", "black", "white"]); | ||
this.color = ""; | ||
eventPublisher.subscribe("color:after", (color) => { | ||
this.color = color; | ||
}); | ||
eventPublisher.publish("color", "red"); | ||
|
||
this.color = ""; | ||
eventPublisher.subscribe("color:after", (color) => { | ||
this.color = color; | ||
}); | ||
eventPublisher.publish("color", "red"); | ||
this.lineWidth = 0; | ||
eventPublisher.subscribe("lineWidth:after", (lineWidth) => { | ||
this.lineWidth = lineWidth; | ||
}); | ||
eventPublisher.publish("lineWidth", 10); | ||
} | ||
} | ||
|
||
this.lineWidth = 0; | ||
eventPublisher.subscribe("lineWidth:after", (lineWidth) => { | ||
this.lineWidth = lineWidth; | ||
}); | ||
eventPublisher.publish("lineWidth", 10); | ||
}; | ||
export default DrawingConfiguration; |
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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
function Frame() { | ||
this.imageData = null; | ||
class Frame { | ||
constructor() { | ||
this.imageData = null; | ||
} | ||
} | ||
|
||
|
||
|
||
export default Frame; |
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 |
---|---|---|
@@ -1,109 +1,111 @@ | ||
import eventPublisher from "./publisher"; | ||
|
||
let isMouseDown = false; | ||
let previousMousePosition; | ||
// HTMLCanvasElementをラップし, canvasRenderingContext2Dに関する操作を提供する | ||
function PaintManager(element) { | ||
let changeDrawState; | ||
class PaintManager{ | ||
constructor() { | ||
let changeDrawState; | ||
|
||
this.element = element; | ||
this.element.width = window.innerWidth; | ||
this.element.height = window.innerHeight; | ||
this.element = element; | ||
this.element.width = window.innerWidth; | ||
this.element.height = window.innerHeight; | ||
|
||
this.element.addEventListener("mousedown", | ||
event => { this.mouseDownCanvas(event); }); | ||
this.element.addEventListener("mouseup", () => { this.mouseUpCanvas(); }); | ||
this.element.addEventListener("mousemove", | ||
event => { this.mouseMoveCanvas(event); }); | ||
this.element.addEventListener("mousedown", | ||
event => { this.mouseDownCanvas(event); }); | ||
this.element.addEventListener("mouseup", () => { this.mouseUpCanvas(); }); | ||
this.element.addEventListener("mousemove", | ||
event => { this.mouseMoveCanvas(event); }); | ||
|
||
this.context = this.element.getContext("2d"); | ||
this.drawState = "idling"; | ||
this.isLock = false; | ||
this.context = this.element.getContext("2d"); | ||
this.drawState = "idling"; | ||
this.isLock = false; | ||
|
||
changeDrawState = (drawState) => { | ||
this.drawState = drawState; | ||
}; | ||
eventPublisher.subscribe("drawState", changeDrawState); | ||
changeDrawState = (drawState) => { | ||
this.drawState = drawState; | ||
}; | ||
eventPublisher.subscribe("drawState", changeDrawState); | ||
|
||
// drawingConfiguration から、コピーしておく。 | ||
this.color = ""; | ||
eventPublisher.subscribe("color", (color) => { | ||
this.color = color; | ||
}); | ||
// drawingConfiguration から、コピーしておく。 | ||
this.color = ""; | ||
eventPublisher.subscribe("color", (color) => { | ||
this.color = color; | ||
}); | ||
|
||
this.lineWidth = 0; | ||
eventPublisher.subscribe("lineWidth", (lineWidth) => { | ||
this.lineWidth = lineWidth; | ||
}); | ||
|
||
eventPublisher.subscribe("isPlaying", (isPlaying) => { | ||
this.isLock = isPlaying; | ||
}); | ||
} | ||
this.lineWidth = 0; | ||
eventPublisher.subscribe("lineWidth", (lineWidth) => { | ||
this.lineWidth = lineWidth; | ||
}); | ||
|
||
let isMouseDown = false; | ||
let previousMousePosition; | ||
PaintManager.prototype.mouseDownCanvas = function(event) { | ||
if (!this.isLock) { | ||
isMouseDown = true; | ||
previousMousePosition = { x: event.clientX, y: event.clientY }; | ||
eventPublisher.publish("drawState", "drawing"); | ||
eventPublisher.subscribe("isPlaying", (isPlaying) => { | ||
this.isLock = isPlaying; | ||
}); | ||
} | ||
|
||
mouseDownCanvas() { | ||
if (!this.isLock) { | ||
isMouseDown = true; | ||
previousMousePosition = { x: event.clientX, y: event.clientY }; | ||
eventPublisher.publish("drawState", "drawing"); | ||
} | ||
} | ||
}; | ||
PaintManager.prototype.mouseUpCanvas = function() { | ||
if (!this.isLock) { | ||
isMouseDown = false; | ||
eventPublisher.publish("drawState", "idling"); | ||
|
||
mouseUpCanvas(){ | ||
if (!this.isLock) { | ||
isMouseDown = false; | ||
eventPublisher.publish("drawState", "idling"); | ||
} | ||
} | ||
}; | ||
PaintManager.prototype.mouseMoveCanvas = function(event) { | ||
if (!this.isLock) { | ||
if (isMouseDown) { | ||
if (this.color === "white") { | ||
this.eraseByLine( | ||
previousMousePosition, | ||
{ x: event.clientX, y: event.clientY }, | ||
this.lineWidth | ||
); | ||
} else { | ||
this.drawLine( | ||
previousMousePosition, | ||
{ x: event.clientX, y: event.clientY }, | ||
this.color, | ||
this.lineWidth | ||
); | ||
mouseMoveCanvas() { | ||
if (!this.isLock) { | ||
if (isMouseDown) { | ||
if (this.color === "white") { | ||
this.eraseByLine( | ||
previousMousePosition, | ||
{ x: event.clientX, y: event.clientY }, | ||
this.lineWidth | ||
); | ||
} else { | ||
this.drawLine( | ||
previousMousePosition, | ||
{ x: event.clientX, y: event.clientY }, | ||
this.color, | ||
this.lineWidth | ||
); | ||
} | ||
previousMousePosition = { x: event.clientX, y: event.clientY }; | ||
} | ||
previousMousePosition = { x: event.clientX, y: event.clientY }; | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* 線を描きます。 | ||
*/ | ||
PaintManager.prototype.drawLine = function( | ||
/** | ||
* 線を描きます。 | ||
*/ | ||
drawLine( | ||
startPosition, | ||
endPosition, | ||
strokeColor, | ||
lineWidth) { | ||
endPosition, | ||
strokeColor, | ||
lineWidth) { | ||
|
||
this.context.strokeStyle = strokeColor; | ||
this.context.lineWidth = lineWidth; | ||
this.context.beginPath(); | ||
this.context.moveTo(startPosition.x, startPosition.y); | ||
this.context.lineTo(endPosition.x, endPosition.y); | ||
this.context.lineCap = "round"; | ||
this.context.stroke(); | ||
}; | ||
this.context.strokeStyle = strokeColor; | ||
this.context.lineWidth = lineWidth; | ||
this.context.beginPath(); | ||
this.context.moveTo(startPosition.x, startPosition.y); | ||
this.context.lineTo(endPosition.x, endPosition.y); | ||
this.context.lineCap = "round"; | ||
this.context.stroke(); | ||
} | ||
|
||
/** | ||
* 線で消します。消した部分は透明になります。 | ||
*/ | ||
PaintManager.prototype.eraseByLine = function( | ||
startPosition, | ||
endPosition, | ||
lineWidth) { | ||
this.context.globalCompositeOperation = "destination-out"; | ||
this.drawLine(startPosition, endPosition, "#000", lineWidth); | ||
this.context.globalCompositeOperation = "source-over"; | ||
}; | ||
|
||
/** | ||
* 線で消します。消した部分は透明になります。 | ||
*/ | ||
eraseByLine( | ||
startPosition, | ||
endPosition, | ||
lineWidth) { | ||
this.context.globalCompositeOperation = "destination-out"; | ||
this.drawLine(startPosition, endPosition, "#000", lineWidth); | ||
this.context.globalCompositeOperation = "source-over"; | ||
} | ||
} | ||
export default PaintManager; |
Oops, something went wrong.