Paperjs:在下部div的中心绘制一个圆圈不遵守正确的中心/Y坐标
jsfiddle: https://jsfiddle.net/09a7kb32/
我有一个页面带有标头div(高度:50px)和一个帆布div(其高度占据屏幕的其余部分)。我正在使用Paperjs绘制一个圆圈,即(下)帆布div的全尺寸。但是,即使应该正确计算中心的X/y坐标似乎也没有遵守。我的圆圈比应有的圆圈要低,并且标头和画布之间有空的垂直缝隙,而它应该直接触摸标头。
canvasElem = $('#canvas').get(0);
paper.setup(canvasElem);
var offset = $('#canvasPanel').offset();
var width = $('#canvasPanel').width();
var height = $('#canvasPanel').height();
var centerX = offset.left + width / 2; // CenterX is left offset of Header DIV (actually 0) + CanvasPanel's half-width
var centerY = offset.top + height / 2; // CenterY is top offset of Header DIV (15px) + CanvasPanel's half-height
// Let the radius be the smaller of the width/height, divided by 2
var radius = Math.min(width,height) / 2;
var circle = new paper.Path.Circle(new paper.Point(centerX,centerY), radius);
circle.fillColor = 'white';
circle.strokeColor = 'black';
paper.view.draw();
CSS:要使顶部/底部divs跨越整个垂直屏幕,请使用绝对定位,如建议在这里。同样,画布本身为100%/100%。
#headerPanel {
border-bottom: 1px solid black;
width: 100%;
height: 50px;
}
#canvasPanel {
position: absolute;
width:100%;
top:50px;
bottom:0;
}
#canvas {
width: 100%;
height: 100%;
}
html
<div id="headerPanel">
Text
</div>
<div id="canvasPanel">
<canvas id="canvas" resize></canvas>
</div>
JSFiddle: https://jsfiddle.net/09a7kb32/
I have a page with a Header DIV (height: 50px) and a Canvas DIV (whose height occupies the remainder of the screen). I'm using PaperJS to draw a circle which is the full size of the (lower) Canvas DIV. However, the center X/Y coordinate doesn't seem to be obeyed, even though it should be getting calculated correctly. I'm getting the circle drawn lower than it should be, with an empty vertical gap between the Header and the Canvas, whereas it should be touching the header directly.
canvasElem = $('#canvas').get(0);
paper.setup(canvasElem);
var offset = $('#canvasPanel').offset();
var width = $('#canvasPanel').width();
var height = $('#canvasPanel').height();
var centerX = offset.left + width / 2; // CenterX is left offset of Header DIV (actually 0) + CanvasPanel's half-width
var centerY = offset.top + height / 2; // CenterY is top offset of Header DIV (15px) + CanvasPanel's half-height
// Let the radius be the smaller of the width/height, divided by 2
var radius = Math.min(width,height) / 2;
var circle = new paper.Path.Circle(new paper.Point(centerX,centerY), radius);
circle.fillColor = 'white';
circle.strokeColor = 'black';
paper.view.draw();
CSS: To get the top/bottom DIVs to span the entire vertical screen, absolute positioning is used, as suggested here. Also, the canvas itself is 100%/100%.
#headerPanel {
border-bottom: 1px solid black;
width: 100%;
height: 50px;
}
#canvasPanel {
position: absolute;
width:100%;
top:50px;
bottom:0;
}
#canvas {
width: 100%;
height: 100%;
}
HTML
<div id="headerPanel">
Text
</div>
<div id="canvasPanel">
<canvas id="canvas" resize></canvas>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想在Paperjs View(Canvas)的中心绘制一个圆圈,那么我将如何做:
您试图将派生的值从Canvas CSS位置和宽度派生到Paperjs View(Canvas),而我不在认为是1x1比较。
If you would like to draw a circle in the center of a paperjs View(canvas), heres how I would do it:
You are trying to apply values derived from the canvas css position and width to the paperjs view(canvas) which I dont believe is a 1x1 comparison.