如何在光标所在的位置绘制线条?HTML5画布使用MrDoob's和谐
How can I get the line to draw where the cursor is positioned? HTML5 canvas using MrDoob's Harmony
问题
如何在光标所在的位置绘制线条?
详细信息
我正试着用杜布先生的和声脚本在html5画布上徒手画一条线(https://github.com/mshuler/harmony)。我对画布进行了编辑,使其具有特定的宽度和高度(在覆盖整个页面之前)。然后我使用以下CSS 定位它
canvas
{
position:absolute;
top:0px; left:0px;
}
当我在画布上绘制(使用鼠标)时,线条会按照正常方式绘制,但如果我像一样定位画布
canvas
{
position:absolute;
top:100px; left:0px;
}
该行显示为低于光标的位置。
问题:如何在光标所在的位置绘制线条
这是我的密码。
CSS
canvas
{
position:absolute;
top:100px; left:0px;
}
#writepad
{
display:block;
width:700px;
height:500px;
}
JAVASCRIPT(Main.js)
SCREEN_HEIGHT = $('#writepad').innerHeight();// SCREEN_HEIGHT = window.innerHeight,
init();
function init()
{
var hash, palette, embed, localStorageImage;
document.body.style.backgroundRepeat = 'no-repeat';
document.body.style.backgroundPosition = 'center center';
container = document.createElement('div');
document.body.appendChild(container);
canvas = document.createElement("canvas");
canvas.width = SCREEN_WIDTH;
canvas.height = SCREEN_HEIGHT;
canvas.style.cursor = 'crosshair';
container.appendChild(canvas);
context = canvas.getContext("2d");
flattenCanvas = document.createElement("canvas");
flattenCanvas.width = SCREEN_WIDTH;
flattenCanvas.height = SCREEN_HEIGHT;
}
// WINDOW
function onWindowMouseMove( event )
{
mouseX = event.clientX;
//console.log("mouseX=",mouseX);
mouseY = event.clientY;
//console.log("mouseY=",mouseY);
}
function onWindowResize()
{
SCREEN_WIDTH = window.innerWidth;
SCREEN_HEIGHT = window.innerHeight;
menu.container.style.left = ((SCREEN_WIDTH - menu.container.offsetWidth) / 2) + 'px';
about.container.style.left = ((SCREEN_WIDTH - about.container.offsetWidth) / 2) + 'px';
about.container.style.top = ((SCREEN_HEIGHT - about.container.offsetHeight) / 2) + 'px';
}
function onWindowBlur( event )
{
shiftKeyIsDown = false;
altKeyIsDown = false;
}
// CANVAS
function onCanvasMouseDown( event )
{
var data, position;
clearTimeout(saveTimeOut);
cleanPopUps();
if (altKeyIsDown)
{
flatten();
data = flattenCanvas.getContext("2d").getImageData(0, 0, flattenCanvas.width, flattenCanvas.height).data;
position = (event.clientX + (event.clientY * canvas.width)) * 4;
console.log("position=",position);
foregroundColorSelector.setColor( [ data[position], data[position + 1], data[position + 2] ] );
return;
}
BRUSH_PRESSURE = wacom && wacom.isWacom ? wacom.pressure : 1;
brush.strokeStart( event.clientX, event.clientY );
window.addEventListener('mousemove', onCanvasMouseMove, false);
window.addEventListener('mouseup', onCanvasMouseUp, false);
}
function onCanvasMouseMove( event )
{
BRUSH_PRESSURE = wacom && wacom.isWacom ? wacom.pressure : 1;
brush.stroke( event.clientX, event.clientY );
}
function onCanvasMouseUp()
{
brush.strokeEnd();
window.removeEventListener('mousemove', onCanvasMouseMove, false);
window.removeEventListener('mouseup', onCanvasMouseUp, false);
}
function onCanvasTouchStart( event )
{
cleanPopUps();
if(event.touches.length == 1) {
event.preventDefault();
brush.strokeStart( event.touches[0].pageX, event.touches[0].pageY );
window.addEventListener('touchmove', onCanvasTouchMove, false);
window.addEventListener('touchend', onCanvasTouchEnd, false);
}
}
function onCanvasTouchMove( event )
{
if(event.touches.length == 1)
{
event.preventDefault();
brush.stroke( event.touches[0].pageX, event.touches[0].pageY );
}
}
function onCanvasTouchEnd( event )
{
if(event.touches.length == 0)
{
event.preventDefault();
brush.strokeEnd();
window.removeEventListener('touchmove', onCanvasTouchMove, false);
window.removeEventListener('touchend', onCanvasTouchEnd, false);
}
}
function flatten()
{
var context = flattenCanvas.getContext("2d");
context.fillStyle = 'rgb(' + BACKGROUND_COLOR[0] + ', ' + BACKGROUND_COLOR[1] + ', ' + BACKGROUND_COLOR[2] + ')';
context.fillRect(0, 0, canvas.width, canvas.height);
context.drawImage(canvas, 0, 0);
}
你想要这样的东西吗???
<!doctype html>
<html>
<head>
<script type="text/javascript">
window.onload=function() {
var canvas=document.getElementById('mycanvas');
var ctx=canvas.getContext('2d');
canvas.addEventListener('mousemove',function onMouseover(e) {
var mx = e.clientX - 8;
var my = e.clientY - 8;
ctx.fillStyle = '#000';
ctx.font = 'bold 22px verdana';
ctx.fillText(".", mx, my,1000);
}, 0);
};
</script>
</head>
<body>
<canvas id="mycanvas" height="400" width="800" style="border:1px solid #000000;">
</body>
</html>
使用偏移属性
y = event.pageY - canvas.offsetTop;
x = event.pageX - canvas.offsetLeft;
相关文章:
- 在指令控制器中使用$attrs时出现问题
- 如何使用jQuery自动打开页面上的所有链接
- 如何使用url加载程序在webpack中导入多个图像
- 使用php或javascript从facebook相册URL中删除多余的部分
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何使用jquery在填充自动完成的值后使文本框只读
- 使用agility.js进行页面布局和合成
- 使用Clipboard.js复制span文本
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何使用WCF服务和javascript表单post上传.doc文件
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 如何使用动画实现纸张推车
- 如何在Angular2中使用jQuery插件
- 使用Express捕获参数
- 使用clickToggle并在单击另一个元素时关闭元素
- 如何使用jQuery选择下拉列表的值
- 可以前端maven插件使用节点,npm已经安装
- 如何将Node.js与卡布奇诺一起使用
- 如何在光标所在的位置绘制线条?HTML5画布使用MrDoob's和谐
- 祖尔布基金会 使用 12 列布局的任一侧的空间