Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jscanvas-Can';不要画平滑的线条_Javascript_Html_Canvas_Drawing - Fatal编程技术网

Javascript jscanvas-Can';不要画平滑的线条

Javascript jscanvas-Can';不要画平滑的线条,javascript,html,canvas,drawing,Javascript,Html,Canvas,Drawing,我一直试图在画布上画出平滑的线条,但我运气不好。我尝试过将lineCap设置为round,使用二次型而不是lineTo,但没有任何效果 当鼠标移动时,它将鼠标的位置记录到一个数组中,然后在数组中运行以绘制线 **JavaScript** // GLOBAL VARIABLES - CONTAINER/CANVAS var wContainer = document.getElementById('Whiteboard_Container'); var layerGrid = documen

我一直试图在画布上画出平滑的线条,但我运气不好。我尝试过将lineCap设置为round,使用二次型而不是lineTo,但没有任何效果

当鼠标移动时,它将鼠标的位置记录到一个数组中,然后在数组中运行以绘制线

**JavaScript**
// GLOBAL VARIABLES - CONTAINER/CANVAS
var wContainer  = document.getElementById('Whiteboard_Container');
var layerGrid   = document.getElementById('LayerGrid');
var lgctx       = layerGrid.getContext('2d');
var layer1      = document.getElementById('Layer1');
var l1ctx       = layer1.getContext('2d');

// GLOBAL VARIABLES - MOUSE/TOUCH
var cursorPos   = { top: 0, left: 0, x: 0, y: 0 };
var cursorType  = 'mouse';  // USED TO CONTROL MOUSE/TOUCH
var dragging    = false;    // SETS/STOPS DRAGGING CANVAS
var dragLock    = false;    // USED TO PREVENT ACCIDENTAL DRAGGING (esp. touch)

// GLOBAL VARIABLES - DRAWING
var layer   = 'LAYER1';
var vectors = [];

// MOUSE EVENT LISTENERS
layer1.addEventListener('mousedown', mouseDown);
layer1.addEventListener('mouseup', mouseUp);

// TOUCH EVENT LISTENERS
layer1.addEventListener('touchstart', touchDown);
layer1.addEventListener('touchend', touchUp);

// INITIALISER
function init()
{
    resize();

    // SET PEN STYLE
    lgctx.strokeStyle = 'rgba(230, 230, 230, 1)';
    lgctx.lineWidth   = 1;
    lgctx.lineCap     = 'round';
    lgctx.lineJoin    = 'round';

    // SET PEN STYLE
    l1ctx.strokeStyle = 'rgba(0, 0, 0, 1)';
    l1ctx.lineWidth   = 10;
    l1ctx.lineCap     = 'round';
    l1ctx.lineJoin    = 'round';
}
init();

// AUTO SIZE THE CANVAS
function resize()
{
    // SET THE CANVAS SIZE
    layerGrid.width     = window.innerWidth * 4;
    layerGrid.height    = window.innerHeight * 4;
    layer1.height       = window.innerHeight * 4;
    layer1.width        = window.innerWidth * 4;
    layer1.height       = window.innerHeight * 4;
}

// MOUSE DOWN
function mouseDown(e)
{
    // Set cursor type
    cursorType = 'mouse';

    // EMPTY CURRENT VECTOR LIST
    vectors = [];
    vectors.length = 0;

    if(e.button == 0) // LEFT MOUSE CLICK
    {
        layer1.addEventListener('mousemove', setVectors);
    }
    else if(e.button == 2 && dragLock == false) // RIGHT MOUSE CLICK
    {
        dragging = true;
        layer1.addEventListener('mousemove', canvasDrag);

        // SET START SCROLL POSITION
        cursorPos.top  = wContainer.scrollTop;
        cursorPos.left = wContainer.scrollLeft;

        // SET CURSOR POSITION
        cursorPos.x = e.clientX;
        cursorPos.y = e.clientY;
    }
}

// MOUSE UP
function mouseUp(e)
{
    // PREVENT DRAGGING & REMOVE GRID
    dragging = false;
    layer1.removeEventListener('mousemove', canvasDrag);
    layer1.removeEventListener('mousemove', setVectors);
    removeGrid();
}

// TOUCH DOWN
function touchDown(e)
{
    e.preventDefault();

    // Set cursor type
    cursorType = 'touch';

    // EMPTY CURRENT VECTOR LIST
    vectors = [];
    vectors.length = 0;

    if(e.touches.length == 1)       // SINGLE TOUCH
    {
        layer1.addEventListener('touchmove', setVectors);
    }
    else if(e.touches.length > 1 && dragLock == false)  // MULTI-TOUCH
    {
        dragging = true;
        layer1.addEventListener('touchmove', canvasDrag);

        // SET START SCROLL POSITION
        cursorPos.top  = wContainer.scrollTop;
        cursorPos.left = wContainer.scrollLeft;

        // SET CURSOR POSITION
        cursorPos.x = e.touches[0].clientX;
        cursorPos.y = e.touches[0].clientY;
    }
}

// TOUCH UP
function touchUp(e)
{
    // PREVENT DRAGGING & REMOVE GRID
    dragging = false;
    layer1.removeEventListener('mousemove', canvasDrag);
    layer1.removeEventListener('touchmove', setVectors);
    removeGrid();
}

// HANDLE MOVEMENT
function canvasDrag(e)
{
    if(dragging == true && cursorType == 'mouse')   // MOUSE DRAG
    {
        // DRAW GRID AS GUIDANCE
        drawGrid();

        const dx = e.clientX - cursorPos.x;
        const dy = e.clientY - cursorPos.y;

        wContainer.scrollTop  = cursorPos.top - dy;
        wContainer.scrollLeft = cursorPos.left - dx;
    }
    else if(dragging == true && cursorType == 'touch' && e.touches.length > 1) // TOUCH DRAG
    {
        // DRAW GRID AS GUIDANCE
        drawGrid();

        const dx = e.touches[0].clientX - cursorPos.x;
        const dy = e.touches[0].clientY - cursorPos.y;

        wContainer.scrollTop  = cursorPos.top - dy;
        wContainer.scrollLeft = cursorPos.left - dx;
    }
}

// SET VECTORS
function setVectors(e)
{
    if(cursorType == 'mouse') // MOUSE DRAW
    {
        // Set the drawing vectors
        vectors.push({x:e.clientX + wContainer.scrollLeft, y:e.clientY + wContainer.scrollTop});
    }
    else if(cursorType == 'touch' && e.touches.length == 1) // ONLY ALLOW IF SINGLE TOUCH
    {
        // Set the drawing vectors
        vectors.push({x:e.touches[0].clientX + wContainer.scrollLeft, y:e.touches[0].clientY + wContainer.scrollTop});
    }

    // Draw the path, based on the vectors array
    drawVectors();
}

// DRAW VECTORS
function drawVectors()
{
    l1ctx.beginPath();
    //l1ctx.clearRect(0, 0, layer1.width, layer1.height);
    for(var i = 0; i < vectors.length - 2; i++)
    {
        var xc = (vectors[i].x + vectors[i + 1].x) / 2;
        var yc = (vectors[i].y + vectors[i + 1].y) / 2;
        l1ctx.quadraticCurveTo(vectors[i].x, vectors[i].y, xc, yc);
        //l1ctx.lineTo(vectors[i].x, vectors[i].y);
    }
    l1ctx.stroke();
}

// DRAW GRID
function drawGrid()
{
    lgctx.beginPath();
    for(var i = 0; i < layer1.width; i = i + 100)
    {
        lgctx.moveTo(i, 0); 
        lgctx.lineTo(i, layer1.height);
    }
    lgctx.moveTo(0, 0);
    for(var i = 0; i < layer1.height; i = i + 100)
    {
        lgctx.moveTo(0, i); 
        lgctx.lineTo(layer1.width, i);
    }
    lgctx.stroke();
    lgctx.closePath();
}

// REMOVE GRID
function removeGrid()
{
    lgctx.clearRect(0, 0, layerGrid.width, layerGrid.height);
}
**JavaScript**
//全局变量-容器/画布
var wContainer=document.getElementById(“白板容器”);
var layerGrid=document.getElementById('layerGrid');
var lgctx=layerGrid.getContext('2d');
var layer1=document.getElementById('layer1');
var l1ctx=layer1.getContext('2d');
//全局变量-鼠标/触摸
var cursorPos={top:0,left:0,x:0,y:0};
var cursorType='鼠标';//用于控制鼠标/触摸
var dragging=false;//设置/停止拖动画布
var dragLock=false;//用于防止意外拖动(尤其是触摸)
//全局变量-绘图
变量层='LAYER1';
var向量=[];
//鼠标事件侦听器
第1层。addEventListener(“鼠标向下”,鼠标向下);
第1层。addEventListener('mouseup',mouseup);
//触摸事件侦听器
第1层。添加了“触控启动”,触地;
第1层。添加的事件监听器(“touchend”,touchUp);
//初始化器
函数init()
{
调整大小();
//笔势
lgctx.strokeStyle='rgba(230,230,230,1)';
lgctx.lineWidth=1;
lgctx.lineCap='圆形';
lgctx.lineJoin='round';
//笔势
l1ctx.strokeStyle='rgba(0,0,0,1)';
l1ctx.lineWidth=10;
l1ctx.lineCap='圆形';
l1ctx.lineJoin='round';
}
init();
//自动调整画布的大小
函数resize()
{
//设置画布大小
layerGrid.width=window.innerWidth*4;
layerGrid.height=window.innerHeight*4;
layer1.height=window.innerHeight*4;
layer1.width=window.innerWidth*4;
layer1.height=window.innerHeight*4;
}
//鼠标落下
功能鼠标向下(e)
{
//设置光标类型
cursorType='鼠标';
//空当前向量列表
向量=[];
向量长度=0;
如果(e.button==0)//鼠标左键单击
{
layer1.addEventListener('mousemove',setVectors);
}
else if(e.button==2&&dragLock==false)//鼠标右键单击
{
拖动=真;
layer1.addEventListener('mousemove',canvasDrag);
//设置开始滚动位置
cursorPos.top=wContainer.scrollTop;
cursorPos.left=wContainer.scrollLeft;
//设置光标位置
cursorPos.x=e.clientX;
cursorPos.y=e.clientY;
}
}
//鼠标移动
功能鼠标(e)
{
//防止拖动和删除网格
拖动=假;
layer1.removeEventListener('mousemove',canvasDrag);
layer1.removeEventListener('mousemove',setVectors);
removeGrid();
}
//着陆
功能接地(e)
{
e、 预防默认值();
//设置光标类型
游标类型='touch';
//空当前向量列表
向量=[];
向量长度=0;
如果(e.touchs.length==1)//单触
{
layer1.addEventListener('touchmove',setVectors);
}
如果(e.touchs.length>1&&dragLock==false)//多点触摸
{
拖动=真;
layer1.addEventListener('touchmove',canvasDrag);
//设置开始滚动位置
cursorPos.top=wContainer.scrollTop;
cursorPos.left=wContainer.scrollLeft;
//设置光标位置
cursorPos.x=e.touchs[0].clientX;
cursorPos.y=e.touchs[0].clientY;
}
}
//修补
功能修补(e)
{
//防止拖动和删除网格
拖动=假;
layer1.removeEventListener('mousemove',canvasDrag);
layer1.removeEventListener('touchmove',setVectors);
removeGrid();
}
//手柄运动
功能画布(e)
{
if(拖动==true&&cursorType==mouse')//鼠标拖动
{
//绘制网格作为指导
drawGrid();
常数dx=e.clientX-cursorPos.x;
常数dy=e.clientY-cursorPos.y;
wContainer.scrollTop=cursorPos.top-dy;
wContainer.scrollLeft=cursorPos.left-dx;
}
else if(拖动==true&&cursorType=='touch'&&e.touchs.length>1)//触摸拖动
{
//绘制网格作为指导
drawGrid();
const dx=e.touchs[0].clientX-cursorPos.x;
const dy=e.touchs[0].clientY-cursorPos.y;
wContainer.scrollTop=cursorPos.top-dy;
wContainer.scrollLeft=cursorPos.left-dx;
}
}
//集合向量
函数集向量(e)
{
if(cursorType=='mouse')//鼠标绘制
{
//设置图形向量
push({x:e.clientX+wContainer.scrollLeft,y:e.clientY+wContainer.scrollTop});
}
else if(cursorType='touch'&&e.touchs.length==1)//仅允许单次触摸
{
//设置图形向量
push({x:e.touchs[0].clientX+wContainer.scrollLeft,y:e.touchs[0].clientY+wContainer.scrollTop});
}
//基于向量数组绘制路径
drawVectors();
}
//绘制向量
函数向量()
{
l1ctx.beginPath();
//l1ctx.clearRect(0,0,layer1.width,layer1.height);
对于(var i=0;i