123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- class Canvas {
- #paint;
- #canvas;
- #context;
- #clickX = new Array();
- #clickY = new Array();
- #clickDrag = new Array();
- constructor(frame_id, canvas_id) {
- let canvas_div = document.getElementById(frame_id);
- this.#canvas = document.createElement('canvas');
- // this.#canvas.setAttribute('style', 'width:100%;height:100%;');
- // this.#canvas.setAttribute('width', 1516);
- // this.#canvas.setAttribute('height', 165);
- this.#canvas.setAttribute('id', canvas_id);
- this.#canvas.setAttribute('class', 'canvas');
- canvas_div.appendChild(this.#canvas);
- if (typeof G_vmlCanvasManager != 'undefined') {
- this.#canvas = G_vmlCanvasManager.initElement(this.#canvas);
- }
- this.#context = this.#canvas.getContext("2d");
- this.attachEvent(canvas_id)
- }
- getCanvasSize() {
- return this.#canvas.getAttribute('height')
- }
- setCanvasSize(width, height) {
- this.#canvas.setAttribute('width', width);
- this.#canvas.setAttribute('height', height);
- }
- getClickX() {
- return [...this.#clickX]
- }
- setClickX(value) {
- this.#clickX = value;
- }
- getClickY() {
- return [...this.#clickY]
- }
- setClickY(value) {
- this.#clickY = value;
- }
- getClickDrag() {
- return [...this.#clickDrag]
- }
- setClickDrag(value) {
- this.#clickDrag = value;
- }
- setCanvasWidth(value) {
- this.#canvas.width = value;
- }
- setCanvasHeight(value) {
- this.#canvas.height = value;
- }
- addClick(x, y, dragging) {
- this.#clickX.push(x);
- this.#clickY.push(y);
- this.#clickDrag.push(dragging);
- // console.log(this.#clickX)
- // console.log(this.#clickY)
- // console.log(this.#clickDrag)
- }
- redraw() {
- this.#context.clearRect(0, 0, this.#context.canvas.width, this.#context.canvas.height); // Clears the canvas
- this.#context.strokeStyle = "#df4b26";
- this.#context.lineJoin = "round";
- this.#context.lineWidth = 2;
- for (var i=0; i < this.#clickX.length; i++) {
- this.#context.beginPath();
- if (this.#clickDrag[i] && i) {
- this.#context.moveTo(this.#clickX[i-1], this.#clickY[i-1]);
- } else {
- this.#context.moveTo(this.#clickX[i]-1, this.#clickY[i]);
- }
- this.#context.lineTo(this.#clickX[i], this.#clickY[i]);
- this.#context.closePath();
- this.#context.stroke();
- }
- }
- clearCanvas() {
- this.#clickX = [];
- this.#clickY = [];
- this.#clickDrag = [];
- this.#context.clearRect(0, 0, this.#canvas.width, this.#canvas.height); //clear canvas
- }
- attachEvent() {
- // PC
- this.#canvas.addEventListener('mousedown', (e)=>{
- let offset = $(e.target).offset()
- let mouseX = e.pageX - this.offsetLeft;
- let mouseY = e.pageY - this.offsetTop;
- this.#paint = true;
- this.addClick(e.pageX - offset.left, e.pageY - offset.top);
- this.redraw();
- });
- this.#canvas.addEventListener('mousemove', (e)=>{
- if (this.#paint) {
- let offset = $(e.target).offset()
- this.addClick(e.pageX - offset.left, e.pageY - offset.top, true);
- this.redraw();
- }
- });
- // 모바일
- this.#canvas.addEventListener('touchstart', (e)=>{
- let offset = $(e.target).offset()
- let mouseX = e.pageX - this.offsetLeft;
- let mouseY = e.pageY - this.offsetTop;
- this.#paint = true;
- this.addClick(e.pageX - offset.left, e.pageY - offset.top);
- this.redraw();
- });
- this.#canvas.addEventListener('touchmove', (e)=>{
- if (this.#paint) {
- const touch = e.touches[0];
- const mouseEvent = new MouseEvent("mousemove", {
- clientX: touch.clientX,
- clientY: touch.clientY
- });
- const offset = $(e.target).offset()
- this.addClick(mouseEvent.pageX - offset.left, mouseEvent.pageY - offset.top, true);
- this.redraw();
- }
- });
- this.#canvas.addEventListener('mouseup', (e)=>{
- this.#paint = false;
- });
- this.#canvas.addEventListener('mouseleave', (e)=>{
- this.#paint = false;
- });
- }
- }
|