在引导模式中创建绘图应用程序

时间:2017-10-31 07:38:54

标签: javascript jquery twitter-bootstrap

我正在尝试在引导模式中制作一个绘图应用。

到目前为止,我已经实现了这个jsfiddle

这是我的代码

AccountManager

但是当单击鼠标时,绘图正在离开鼠标指针的位置。检查小提琴,了解我的意思。

此外,我想知道如何使画布采用模式高度宽度并且传递静态宽度高度,如此var canvasDiv = document.getElementById('canvasDiv'); canvas = document.createElement('canvas'); canvas.setAttribute('width', 570); canvas.setAttribute('height', 300); canvas.setAttribute('id', 'canvas'); canvasDiv.appendChild(canvas); if(typeof G_vmlCanvasManager != 'undefined') { canvas = G_vmlCanvasManager.initElement(canvas); } context = canvas.getContext("2d"); $('#canvas').mousedown(function(e){ var mouseX = e.pageX - this.offsetLeft; var mouseY = e.pageY - this.offsetTop; paint = true; addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop); redraw(); }); $('#canvas').mousemove(function(e){ if(paint){ addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true); redraw(); } }); $('#canvas').mouseup(function(e){ paint = false; }); $('#canvas').mouseleave(function(e){ paint = false; }); var clickX = new Array(); var clickY = new Array(); var clickDrag = new Array(); var paint; function addClick(x, y, dragging) { clickX.push(x); clickY.push(y); clickDrag.push(dragging); } function redraw(){ context.clearRect(0, 0, context.canvas.width, context.canvas.height); // Clears the canvas context.strokeStyle = "#df4b26"; context.lineJoin = "round"; context.lineWidth = 5; for(var i=0; i < clickX.length; i++) { context.beginPath(); if(clickDrag[i] && i){ context.moveTo(clickX[i-1], clickY[i-1]); }else{ context.moveTo(clickX[i]-1, clickY[i]); } context.lineTo(clickX[i], clickY[i]); context.closePath(); context.stroke(); } }

1 个答案:

答案 0 :(得分:1)

1.offsetleft相对于父元素https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetLeft,所以你需要$.offset

2.高度和宽度根据父母显示后显示

结果见https://jsfiddle.net/hj898aqb/1/