【问题标题】:Get x, y coordinate on the 4 corners of an image获取图像 4 个角的 x,y 坐标
【发布时间】:2020-07-05 00:30:56
【问题描述】:

我可以通过以下方式获取图像的坐标 x、y:

var pixelData = context.getImageData(offsetX, offsetY, 1, 1).data;

如何只计算角的坐标?

【问题讨论】:

标签: javascript canvas


【解决方案1】:

您需要的是画布的宽度和高度。在下面的示例中,或者通常假设左上角的图像坐标为 0,0。

const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
context.fillStyle = 'blue';
context.rect(0, 0, 200, 200);
context.fill();

var width = canvas.width;
var height = canvas.height;

var topLeft =[ 0 , 0];
var topRight = [width,0];
var bottomLeft = [0,height];
var bottomRight = [width,height];

// If you are getting the pixels on the possible points then use this

var topLeft =[ 0 , 0];
var topRight = [width -1,0];
var bottomLeft = [0,height -1 ];
var bottomRight = [width -1,height-1];
<canvas id="canvas" width="400" height="400"><canvas>

【讨论】:

    猜你喜欢
    • 2021-07-10
    • 1970-01-01
    • 2021-05-14
    • 2019-06-10
    • 1970-01-01
    • 2019-12-26
    • 2011-09-19
    • 1970-01-01
    • 2017-08-11
    相关资源
    最近更新 更多