使用触摸屏设备在画布上绘图

用户随机用户

我一直在进行一些搜索,以查看是否有可能允许触摸屏用户通过原始JavaScript在canvas标签上进行绘制。到目前为止,我运气不佳,最终又回到了无法与之互动的画布上。我的div里面有画布。关于如何实现此目标的任何建议?我已经尝试了一些教程,但它们似乎仅适用于桌面设备和单击事件,而不是在用户在画布上移动手指时不起作用。

瓦鲁纳·曼朱拉(Waruna Manjula)

<html>
<head>
<title>Sketchpad</title>

<script type="text/javascript">
    // Variables for referencing the canvas and 2dcanvas context
    var canvas,ctx;

    // Variables to keep track of the mouse position and left-button status 
    var mouseX,mouseY,mouseDown=0;

    // Draws a dot at a specific position on the supplied canvas name
    // Parameters are: A canvas context, the x position, the y position, the size of the dot
    function drawDot(ctx,x,y,size) {
        // Let's use black by setting RGB values to 0, and 255 alpha (completely opaque)
        r=0; g=0; b=0; a=255;

        // Select a fill style
        ctx.fillStyle = "rgba("+r+","+g+","+b+","+(a/255)+")";

        // Draw a filled circle
        ctx.beginPath();
        ctx.arc(x, y, size, 0, Math.PI*2, true); 
        ctx.closePath();
        ctx.fill();
    } 

    // Clear the canvas context using the canvas width and height
    function clearCanvas(canvas,ctx) {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
    }

    // Keep track of the mouse button being pressed and draw a dot at current location
    function sketchpad_mouseDown() {
        mouseDown=1;
        drawDot(ctx,mouseX,mouseY,12);
    }

    // Keep track of the mouse button being released
    function sketchpad_mouseUp() {
        mouseDown=0;
    }

    // Keep track of the mouse position and draw a dot if mouse button is currently pressed
    function sketchpad_mouseMove(e) { 
        // Update the mouse co-ordinates when moved
        getMousePos(e);

        // Draw a dot if the mouse button is currently being pressed
        if (mouseDown==1) {
            drawDot(ctx,mouseX,mouseY,12);
        }
    }

    // Get the current mouse position relative to the top-left of the canvas
    function getMousePos(e) {
        if (!e)
            var e = event;

        if (e.offsetX) {
            mouseX = e.offsetX;
            mouseY = e.offsetY;
        }
        else if (e.layerX) {
            mouseX = e.layerX;
            mouseY = e.layerY;
        }
     }


    // Set-up the canvas and add our event handlers after the page has loaded
    function init() {
        // Get the specific canvas element from the HTML document
        canvas = document.getElementById('sketchpad');

        // If the browser supports the canvas tag, get the 2d drawing context for this canvas
        if (canvas.getContext)
            ctx = canvas.getContext('2d');

        // Check that we have a valid context to draw on/with before adding event handlers
        if (ctx) {
            canvas.addEventListener('mousedown', sketchpad_mouseDown, false);
            canvas.addEventListener('mousemove', sketchpad_mouseMove, false);
            window.addEventListener('mouseup', sketchpad_mouseUp, false);
        }
    }
</script>

<style>
/* Some CSS styling */
#sketchpadapp {
    /* Prevent nearby text being highlighted when accidentally dragging mouse outside confines of the canvas */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}


#sketchpad {
    float:left;
    border:2px solid #888;
    border-radius:4px;
    position:relative; /* Necessary for correct mouse co-ords in Firefox */
}
</style>
</head>

<body onload="init()">
    <div id="sketchpadapp">
        <div class="leftside1">
             

             <input type="submit" value="Clear Sketchpad" onclick="clearCanvas(canvas,ctx);">
        </div>
        <div class="rightside">
            <canvas id="sketchpad" height="300" width="400">
            </canvas>
        </div>
    </div>
</body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用触摸屏设备在画布上绘图

来自分类Dev

触摸屏上的画布问题

来自分类Dev

触摸屏上的画布问题

来自分类Dev

WPF AutomationPeer在触摸屏设备上崩溃

来自分类Dev

CSS:在触摸屏设备上的悬停行为

来自分类Dev

CSS-模拟:在触摸屏设备上悬停效果

来自分类Dev

悬停伪类在触摸屏设备上的表现如何

来自分类Dev

可以在触摸屏设备上使用p5.js的鼠标变量吗?(JAVASCRIPT)

来自分类Dev

简单的单击事件,可在Ubuntu上与触摸屏设备一起使用

来自分类Dev

如何在Android设备上使用方向控制修复触摸屏事件?

来自分类Dev

pygtk用于触摸屏设备

来自分类Dev

JetBrains IDE在触摸屏上滚动

来自分类Dev

在触摸屏上处理悬停事件

来自分类Dev

CWAC相机在触摸屏上拍照

来自分类Dev

触摸屏上难以点击的链接

来自分类Dev

在Debian上配置触摸屏?

来自分类Dev

在Nodewebkit上禁用触摸屏(Windows)

来自分类Dev

在触摸屏上保留悬停效果

来自分类Dev

在触摸屏设备上未调用角ng-click函数

来自分类Dev

缺少“模拟触摸屏”选项,“设备模拟”不提供任何触摸屏响应

来自分类Dev

如何使用触摸屏绘制?

来自分类Dev

使用触摸屏和vi吗?

来自分类Dev

使用触摸屏在Windows 8上的Chrome中检测触摸事件

来自分类Dev

Windows 7多点触摸是否可以在每个触摸屏上使用?

来自分类Dev

触摸屏滚动

来自分类Dev

禁用触摸屏

来自分类Dev

多个触摸屏

来自分类Dev

触摸屏滚动

来自分类Dev

createTouch与ontouchstart-检测触摸屏设备的最佳方法?