Skip to content

Commit

Permalink
prototypeをクラスに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayato Sakurai authored and Ayato Sakurai committed May 7, 2016
1 parent 328b324 commit dd36df9
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 169 deletions.
33 changes: 16 additions & 17 deletions js/canvas-model.js
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;
57 changes: 30 additions & 27 deletions js/drawing-configuration.js
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;
8 changes: 6 additions & 2 deletions js/frame.js
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;
42 changes: 25 additions & 17 deletions js/frames-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Frame from "./frame";
import eventPublisher from "./publisher";
import CanvasModel from "./canvas-model";

// frame の追加・削除、currentFrameの切り替えをModel上で行う
function FramesController(canvas) {
class FramesController{
constructor() {
let updateImageDataToNextData;
this.frames = [];
this.currentFrameId = 0;
Expand All @@ -19,16 +19,13 @@ function FramesController(canvas) {
};
eventPublisher.subscribe("currentFrameId", updateImageDataToNextData);
}

// パラメータ id : どこの後ろに追加するのか(今は実装していない)
FramesController.prototype.append = function(id) {
const frame = new Frame();
append() {
const frame = new Frame();
// 今はいいが、あとで splice に変える
this.frames.push(frame);
eventPublisher.publish("frames", this.frames);
};

FramesController.prototype.remove = function(id) {
}
remove() {
if (this.frames.length <= 1) {
throw new Error("残りフレーム数が1なので、削除することができません。");
}
Expand All @@ -43,15 +40,26 @@ FramesController.prototype.remove = function(id) {
eventPublisher.publish("currentFrameId", nextCurrentFrameId);
};

FramesController.prototype.setCurrentFrame = function(frameId) {
setCurrentFrame(){
eventPublisher.publish("currentFrameId", frameId);
};

FramesController.prototype.getFrameById = function(frameId) {
return this.frames[frameId];
};
}
getFrameById(){
return this.frames[frameId];
}

FramesController.prototype.getCurrentFrame = function() {
getCurrentFrame(){
return this.frames[this.currentFrameId];
};
}
}

// frame の追加・削除、currentFrameの切り替えをModel上で行う

// beforeFrameは削除されている可能性がある


// パラメータ id : どこの後ろに追加するのか(今は実装していない)

// 今はいいが、あとで splice に変える

export default FramesController;
180 changes: 91 additions & 89 deletions js/paint-manager.js
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;
Loading

0 comments on commit dd36df9

Please sign in to comment.