Skip to content

Commit

Permalink
resize line
Browse files Browse the repository at this point in the history
  • Loading branch information
rifaldyaristya committed Feb 23, 2021
1 parent 32fff8d commit 4b9ec25
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function canvasClick(e) {
} else {
if (!selectedObject) {
let selected = getAllSelected(translatedMidPoint);
console.log(selected);
selectedObject = selected[selected.length - 1];
} else {
selectedObject = null;
Expand Down Expand Up @@ -164,6 +165,8 @@ function canvasMove(e) {
let translatedMidPoint = translatePointCoordinate(cursor.x, cursor.y);
if (selectedObject instanceof Rectangle) {
selectedObject.resizeRectangle(translatedMidPoint.x);
} else if (selectedObject instanceof Line) {
selectedObject.resizeLine(translatedMidPoint.x, translatedMidPoint.y);
}
// else {
// se
Expand Down
86 changes: 51 additions & 35 deletions line.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
class Line {
constructor(firstPoint, secondPoint, color) {
this.color = color;
this.vertices = [
firstPoint.x, firstPoint.y, 0,
secondPoint.x, secondPoint.y, 0,
];
this.xformMatrix = new Float32Array([
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
]);
}
drawOnCanvas() {
let vertex_buffer = getVerticesBuffer(this.vertices);
let vertShader = getVertexShader();
let fragShader = getFragmentShader(this.color);
let shaderProgram = getShaderProgram(vertShader, fragShader);
transformObject(shaderProgram, this.xformMatrix);
bindVertexBuffer(shaderProgram, vertex_buffer);
gl.drawArrays(gl.LINES, 0, 2);
}
}
constructor(firstPoint, secondPoint, color) {
this.color = color;
this.vertices = [
firstPoint.x,
firstPoint.y,
0,
secondPoint.x,
secondPoint.y,
0,
];
this.xformMatrix = new Float32Array([
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
]);
}
drawOnCanvas() {
let vertex_buffer = getVerticesBuffer(this.vertices);
let vertShader = getVertexShader();
let fragShader = getFragmentShader(this.color);
let shaderProgram = getShaderProgram(vertShader, fragShader);
transformObject(shaderProgram, this.xformMatrix);
bindVertexBuffer(shaderProgram, vertex_buffer);
gl.drawArrays(gl.LINES, 0, 2);
}

resizeLine(xCursor, yCursor) {
let midX = (this.vertices[0] + this.vertices[3]) / 2;
let midY = (this.vertices[1] + this.vertices[4]) / 2;
let length = Math.abs(xCursor - midX);
let height = Math.abs(yCursor - midY);
this.vertices[0] = midX - length / 2;
this.vertices[3] = midX + length / 2;
this.vertices[1] = midY - height / 2;
this.vertices[4] = midY + height / 2;
renderAll();
}
}

0 comments on commit 4b9ec25

Please sign in to comment.