Reputation: 835
Please help me, how to keep content size while resize canvas size ?
Please see this code:
https://jsfiddle.net/thobn24h/79Ls6fry/7/
when I resize windows, Rectangle and Text are scaled and have aliasing. How to make them have same size with original and don't have aliasing ?
( by the way, why at the first time canvas size not equal with div size ? )
Thank you!
<style>
#canvasContainer {
width: 100%;
height: 100vh;
background-color: gray;
}
</style>
<div id="canvasContainer">
<canvas id="editorCanvas"></canvas>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.0/fabric.js"></script>
<script>
$(function() {
var canvasObject = document.getElementById("editorCanvas");
// set canvas equal size with div
$(canvasObject).width($("#canvasContainer").width());
$(canvasObject).height($("#canvasContainer").height());
console.log("width: " + $(canvasObject).width());
console.log("height: " + $(canvasObject).height());
canvas = new fabric.Canvas('editorCanvas', {
backgroundColor: 'white',
selectionColor: 'blue',
selectionLineWidth: 2
});
// create a rectangle object
var rect = new fabric.Rect({
left: 10,
top: 10,
fill: 'red',
width: 150,
height: 150
});
rect.set({
transparentCorners: false,
rotatingPointOffset: 40,
cornerColor: 'black',
cornerStrokeColor: 'black',
borderColor: 'black',
cornerSize: 12,
padding: 10,
cornerStyle: 'circle',
borderDashArray: [3, 3]
});
// "add" rectangle onto canvas
canvas.add(rect);
var text = new fabric.Text('hello world', { left: 10, top: 10 });
canvas.add(text);
// Tracking resize windows event
window.addEventListener('resize', resizeCanvas, false);
});
function resizeCanvas() {
var canvasObject = document.getElementById("editorCanvas");
$(canvasObject).width($("#canvasContainer").width());
$(canvasObject).height($("#canvasContainer").height());
console.log("width: " + $(canvasObject).width());
console.log("height: " + $(canvasObject).height());
}
Upvotes: 2
Views: 2646
Reputation: 15604
You need to use canvas#setDimensions to set widht/height.
DEMO
canvas = new fabric.Canvas('editorCanvas', {
backgroundColor: 'white',
selectionColor: 'blue',
selectionLineWidth: 2,
width: $("#canvasContainer").width(),
height:$("#canvasContainer").height()
});
// create a rectangle object
var rect = new fabric.Rect({
left: 10,
top: 10,
fill: 'red',
width: 150,
height: 150
});
rect.set({
transparentCorners: false,
rotatingPointOffset: 40,
cornerColor: 'black',
cornerStrokeColor: 'black',
borderColor: 'black',
cornerSize: 12,
padding: 10,
cornerStyle: 'circle',
borderDashArray: [3, 3]
});
// "add" rectangle onto canvas
canvas.add(rect);
var text = new fabric.Text('hello world', {
left: 10,
top: 10
});
canvas.add(text);
// Tracking resize windows event
window.addEventListener('resize', resizeCanvas, false);
function resizeCanvas() {
canvas.setDimensions({
width: $("#canvasContainer").width(),
height: $("#canvasContainer").height()
})
}
#canvasContainer {
width: 100%;
height: 100vh;
background-color: gray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.0/fabric.js"></script>
<div id="canvasContainer">
<canvas id="editorCanvas"></canvas>
</div>
Upvotes: 1