gpt4 book ai didi

javascript - 如何给 HTML5 Canvas 色轮添加触摸事件?

转载 作者:太空狗 更新时间:2023-10-29 16:41:35 26 4
gpt4 key购买 nike

我正在尝试使用 HTML5 Canvas 色轮为 iOS 移动设备创建触摸事件,但无法正常工作。

我使用的代码:http://www.script-tutorials.com/html5-color-picker-canvas/

我将 mousemove 事件更改为 touch move 但这不起作用。我做错了什么?

    /**
*
* 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 色轮标记:

<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 不是有效的事件名称。

您可能想改用touch

关于javascript - 如何给 HTML5 Canvas 色轮添加触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27484107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com