admin管理员组文章数量:1389758
I'm using FabricJS to create a fullscreen canvas and save the changes serverside. Before loading it I resize the canvas & background so that on every page-load the canvas fits inside the users window.
canvas.setHeight($(window).height());
canvas.setWidth($(window).width());
canvas.backgroundImage.width = $(window).width();
canvas.backgroundImage.height = $(window).height();
The problem is that the canvas & background get adjusted but not the objects. They keep the same position relative to the screen and don't change size so are incorrent on other screensizes. How can this problem be solved?
I'm using FabricJS to create a fullscreen canvas and save the changes serverside. Before loading it I resize the canvas & background so that on every page-load the canvas fits inside the users window.
canvas.setHeight($(window).height());
canvas.setWidth($(window).width());
canvas.backgroundImage.width = $(window).width();
canvas.backgroundImage.height = $(window).height();
The problem is that the canvas & background get adjusted but not the objects. They keep the same position relative to the screen and don't change size so are incorrent on other screensizes. How can this problem be solved?
Share Improve this question asked Jan 19, 2016 at 10:50 SamSam 1,3433 gold badges24 silver badges41 bronze badges1 Answer
Reset to default 8After a lot of trying I found a solution thanks to a hint from AndreaBogazzi. When changing the background/canvas size/position the objects do not keep there size/position relative to the background/canvas.
That's why it's better to use the zoom functionality, this way all of the canvas elements gets adjusted the same way relative to each other:
// background: background image
// zoom: zoom amount usable with canvas.setZoom(zoom).
if (background.width < $(window).width()) {
zoom = $(window).height() / background.height;
if ((zoom * background.width) > $(window).width()) {
zoom = $(window).width() / background.width;
}
} else {
zoom = $(window).width() / background.width;
if ((zoom * background.height) > $(window).height()) {
zoom = $(window).height() / background.height;
}
}
canvas.setZoom(zoom );
This will result in the canvas always being it's maximum size without breaking ratio, without leaving the screen and with all the elements keeping the right position and height.
本文标签: javascriptFabricJS objects not relative to canvas resizeStack Overflow
版权声明:本文标题:javascript - FabricJS objects not relative to canvas resize - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744602260a2615138.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论