gpt4 book ai didi

javascript - html5 Canvas ,在溢出时添加滚动条

转载 作者:行者123 更新时间:2023-11-28 02:53:20 24 4
gpt4 key购买 nike

你好,我有一个 html Canvas ,我想添加一个滚动条,就像任何网页或文本区域一样,我使用了 overflow: scroll;但它只显示滚动条并且它们被禁用(我无法滚动)

这是标记

<div class="ccsp-area">
<canvas id="mainCanvas" width="900" height="500"></canvas>
</div>

这是 css (scss)

 .ccsp-area {
width: 90%;
height: 100%;
display: inline-block;
canvas {
display: inline-block;
background-color: #fff;
max-width: 100% !important;
overflow: scroll;
}
}

最后是 JS

var canvas = $("#mainCanvas");
var ctx = canvas[0].getContext('2d');
var targetSizeE = $(".ccsp-area");
var rwidth = targetSizeE.width() -200;
var rheight = targetSizeE.height() -80;
// no need to read more code after this stage
canvas.attr('width', rwidth);
canvas.attr('height', rheight);
ctx.beginPath();
ctx.moveTo(100 , 100);
ctx.lineTo(600, 600);
ctx.lineTo(600,100);
ctx.closePath();
ctx.lineWidth = 20;
ctx.strokeStyle = "rgba(10,220,50,1)";
ctx.fillStyle = "rgba(10,220,50,0.5)";
ctx.stroke();

结果截图 result

如您所见,滚动条被禁用,即使 Canvas 内的绘图超过 Canvas 的高度,我也无法滚动。

最佳答案

正如那家伙所说, Canvas 没有内容可以溢出所以最好的方法是将 Canvas 的宽度和高度设置为非常大的东西,比如 2200 x 1500(你可以通过 js 更改高度)

这就是你的 CSS 应该看起来的样子

.ccsp-area {
width: 90%;
height: 100%;
display: inline-block;
overflow: scroll;
canvas {
display: inline-block;
background-color: #fff;
}
}

根据您的情况从 html 元素而不是 JS 设置大小(也可以随意删除设置 Canvas 大小的 js 代码)

<canvas id="mainCanvas" width="900" height="500"></canvas>

关于javascript - html5 Canvas ,在溢出时添加滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38366947/

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