简体   繁体   English

如何将触摸事件添加到HTML5 Canvas色轮?

[英]How to add a touch event to the HTML5 Canvas color wheel?

I am trying to make a touch event for an iOS mobile with the HTML5 Canvas color wheel, but can't get it work. 我正在尝试使用HTML5 Canvas色轮为iOS手机制作触摸事件,但无法使其正常工作。

I use the code of: http://www.script-tutorials.com/html5-color-picker-canvas/ 我使用的代码: http//www.script-tutorials.com/html5-color-picker-canvas/

I changed the mousemove event to touch move but this isn't working. 我更改了mousemove事件以touch move但这不起作用。 What do I wrong? 我错了什么?

    /**
 *
 * HTML5 Color Picker
 *
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Copyright 2012, Script Tutorials
 * http://www.script-tutorials.com/
 */

$(function(){
    var bCanPreview = true; // can preview

    // create canvas and context objects
    var canvas = document.getElementById('picker');
    var ctx = canvas.getContext('2d');

    // drawing active image
    var image = new Image();
    image.onload = function () {
        ctx.drawImage(image, 0, 0, image.width, image.height); // draw the image on the canvas
    }

    // select desired colorwheel
    var imageSrc = 'images/colorwheel1.png';
    image.src = imageSrc;

    $('#picker').touchmove(function(event) { // mouse move handler
        event.preventDefault();

        var touch = event.originalEvent.changedTouches[0];

        if (bCanPreview) {
            // get coordinates of current position
            var canvasOffset = $(canvas).offset();
            var canvasX = Math.floor( touch.pageX - canvasOffset.left);
            var canvasY = Math.floor( touch.pageY - canvasOffset.top);

            // get current pixel
            var imageData = ctx.getImageData(canvasX, canvasY, 1, 1);
            var pixel = imageData.data;

            // update preview color
            var pixelColor = "rgb("+pixel[0]+", "+pixel[1]+", "+pixel[2]+")";
            $('.preview').css('backgroundColor', pixelColor);

            // update controls
            $('#rVal').val(pixel[0]);
            $('#gVal').val(pixel[1]);
            $('#bVal').val(pixel[2]);
            $('#rgbVal').val(pixel[0]+','+pixel[1]+','+pixel[2]);

            var dColor = pixel[2] + 256 * pixel[1] + 65536 * pixel[0];
            $('#hexVal').val('#' + ('0000' + dColor.toString(16)).substr(-6));
        }
    });
    $('#picker').click(function(e) { // click event handler
        bCanPreview = !bCanPreview;
    }); 
    $('.preview').click(function(e) { // preview click
        $('.colorpicker').fadeToggle("slow", "linear");
        bCanPreview = true;
    });
});

HTML5 color wheel markup: HTML5色轮标记:

<div class="container">

        <!-- preview element -->
        <div class="preview"></div>

        <!-- colorpicker element -->
        <div class="colorpicker">
            <canvas id="picker" var="1" width="300" height="300"></canvas>

            <div class="controls" style="display: none;">
                <div><label>R</label> <input type="text" id="rVal" /></div>
                <div><label>G</label> <input type="text" id="gVal" /></div>
                <div><label>B</label> <input type="text" id="bVal" /></div>
                <div><label>RGB</label> <input type="text" id="rgbVal" /></div>
            </div>
        </div>
    </div>

touchmove is not a valid event name. touchmove不是有效的事件名称。

You probably want to use touch , instead. 您可能想要使用touch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM