gpt4 book ai didi

javascript - Canvas 点绘图在调整大小后不缩放

转载 作者:可可西里 更新时间:2023-11-01 12:59:26 24 4
gpt4 key购买 nike

在使用绘制在 Canvas 上的 javascript 点动态调整 Canvas 大小后, Canvas 上的点不再与它们的放置位置对齐,而且圆弧也从它们应有的呈现方式开始拉伸(stretch)。

下面的可运行示例演示了这个问题,因为您单击光标的地方变成了一个大的错形椭圆,它位于错误的位置。

var TargetWidth = 400;

var canvases = $(".hotspot-canvas")

for (i = 0; i < canvases.length; i++) {
canvas = $(canvases[i]);

var src = canvas.attr("data-img");

initilizePlotPointCanvas(canvas, src);

}

function initilizePlotPointCanvas(canvas, src)
{
var my_image = new Image();
my_image.onload = function(){

var w1 = this.width;
var w2 = TargetWidth

var r = w1 / w2;

var h1 = this.height;
var h2 = this.height / r;

canvas.width(TargetWidth).height(h2).css("background-image", "url("+src+")");
setTimeout(function(){

var jcanvas = canvas[0];

var ctx = jcanvas.getContext('2d'),
w = jcanvas.width,
h = jcanvas.height;
ctx.translate(-0.1, -0.1);
jcanvas.onmousedown = function(e) {
var rect = jcanvas.getBoundingClientRect();
x3 = e.clientX - rect.left;
y3 = e.clientY - rect.top;

ctx.clearRect(0, 0, w, h)
ctx.beginPath();
ctx.arc(x3, y3, 5, 0, 2 * Math.PI, false);
ctx.fillStyle = 'red';
ctx.fill();
ctx.stroke();
}

}, 500)

}
my_image.src = src;
}
.hotspot-canvas {
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
cursor: crosshair;
border: 1px solid black;
background-repeat: no-repeat;
background-position: center center;
background-clip: border-box;
background-origin: padding-box;
-moz-background-size: cover;
background-size: cover;
}
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<canvas
data-id="554924"
data-img="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQXXarydhE3CZRSMgXCProd1w0_oSwIOPd7zJN5EQmLtQtPDD21"
class="hotspot-canvas"
data-responses="0" data-notes="0" data-actions="0" data-email="123" data-responder="true" data-response-id="" orig_type="14" data-tag="6bbf8f97-758f-47ca-8b8c-24a1cd3ddd55" data-formtemplatequestiontype="s" data-valueifparentna="" data-exportkey=""></canvas>

最佳答案

显示尺寸和 Canvas 分辨率

显示尺寸和 Canvas 分辨率是两个不同的实体。

当你设置 Canvas 样式的宽度和高度时,你就设置了显示大小

canvas.style.width = "100px";
canvas.style.height = "100px";

当您设置 Canvas 宽度和高度时,您就设置了分辨率。

canvas.width = 100;
canvas.height = 100;

您已经设置了显示尺寸,但忽略了将分辨率与显示尺寸相匹配。

你可以通过设置分辨率来匹配显示大小来修复它

var bounds = jcanvas.getBoundingClientRect();
jcanvas.width = bounds.width;
jcanvas.height = bounds.height;

var TargetWidth = 400;

var canvases = $(".hotspot-canvas")

for (i = 0; i < canvases.length; i++) {
canvas = $(canvases[i]);

var src = canvas.attr("data-img");

initilizePlotPointCanvas(canvas, src);

}

function initilizePlotPointCanvas(canvas, src)
{
var my_image = new Image();
my_image.onload = function(){

var w1 = this.width;
var w2 = TargetWidth

var r = w1 / w2;

var h1 = this.height;
var h2 = this.height / r;

canvas.width(TargetWidth).height(h2).css("background-image", "url("+src+")");

setTimeout(function(){

var jcanvas = canvas[0];
var bounds = jcanvas.getBoundingClientRect();
jcanvas.width = bounds.width;
jcanvas.height = bounds.height;
var ctx = jcanvas.getContext('2d'),
w = jcanvas.width,
h = jcanvas.height;
ctx.translate(-0.1, -0.1);
jcanvas.onmousedown = function(e) {
var rect = jcanvas.getBoundingClientRect();
x3 = e.clientX - rect.left;
y3 = e.clientY - rect.top;

ctx.clearRect(0, 0, w, h)
ctx.beginPath();
ctx.arc(x3, y3, 5, 0, 2 * Math.PI, false);
ctx.fillStyle = 'red';
ctx.fill();
ctx.stroke();
}

}, 500)

}
my_image.src = src;
}
.hotspot-canvas {
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
cursor: crosshair;
border: 1px solid black;
background-repeat: no-repeat;
background-position: center center;
background-clip: border-box;
background-origin: padding-box;
-moz-background-size: cover;
background-size: cover;
}
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<canvas
data-id="554924"
data-img="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQXXarydhE3CZRSMgXCProd1w0_oSwIOPd7zJN5EQmLtQtPDD21"
class="hotspot-canvas"
data-responses="0" data-notes="0" data-actions="0" data-email="123" data-responder="true" data-response-id="" orig_type="14" data-tag="6bbf8f97-758f-47ca-8b8c-24a1cd3ddd55" data-formtemplatequestiontype="s" data-valueifparentna="" data-exportkey=""></canvas>

关于javascript - Canvas 点绘图在调整大小后不缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42539245/

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