gpt4 book ai didi

javascript - Fabricjs获取缩放或平移后的RGB像素值

转载 作者:行者123 更新时间:2023-11-28 04:50:37 25 4
gpt4 key购买 nike

执行平移或缩放后,我无法在 Canvas 上获取正确的鼠标坐标。我有这个用于采样坐标和 RGB 的代码:

        canvas1.on('mouse:move', function (e) {
//allowing pan only if the image is zoomed.
if (panning && e && e.e) {
var delta = new fabric.Point(e.e.movementX, e.e.movementY);
canvas1.relativePan(delta);
} else {//Read the RGB value of the mouse point
var mouse = canvas1.getPointer(e.e);

var x = parseInt(mouse.x);
var y = parseInt(mouse.y);

// get the color array for the pixel under the mouse
var px = ctx.getImageData(x, y, 1, 1).data;

// report that pixel data
results.innerHTML = 'At [' + x + ' / ' + y + ']: Red/Green/Blue/Alpha = [' + px[0] + ' / ' + px[1] + ' / ' + px[2] + ' / ' + px[3] + ']';
}
});

问题是缩放/平移后坐标“错误”,例如左上角不是 (0, 0) 而是 (-someX, -someY)...

任何帮助将不胜感激

编辑:我发现了错误,这会修复它。希望对其他人有用

                var x = e.e.offsetX;//parseInt(mouse.x);
var y = e.e.offsetY;//parseInt(mouse.y);

最佳答案

这段代码解决了这个问题,希望它对其他人有用。

 canvas1.on('mouse:move', function (e) {
//allowing pan only if the image is zoomed.
if (panning && e && e.e) {
var delta = new fabric.Point(e.e.movementX, e.e.movementY);
canvas1.relativePan(delta);
} else {//Read the RGB value of the mouse point
var mouse = canvas1.getPointer(e.e);

var x = e.e.offsetX;//parseInt(mouse.x);
var y = e.e.offsetY;//parseInt(mouse.y);

// get the color array for the pixel under the mouse
var px = ctx.getImageData(x, y, 1, 1).data;

// report that pixel data
results.innerHTML = 'At [' + x + ' / ' + y + ']: Red/Green/Blue/Alpha = [' + px[0] + ' / ' + px[1] + ' / ' + px[2] + ' / ' + px[3] + ']';
}
});

关于javascript - Fabricjs获取缩放或平移后的RGB像素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42953929/

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