I have a very wide image that exceeds mostly viewing widths and must be rendered using a <canvas>
tag. How would I hide the overflow and also center the image?
In other words, I'm looking for the canvas equivalent to background-position: center
.
Ideally, this would be done in a way which is responsive - so if the viewing window is resized, the image stays centered.
Here's an example:
window.onload = function() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var img = document.getElementById("image");
ctx.drawImage(img, 0, 0);
};
.container {
width: 100%;
max-width: 1200px;
margin: 0 auto;
}
canvas {
overflow: hidden;
}
.canvasContainer {
width: 100%;
overflow-x: hidden;
}
img {
display: none;
}
<div class="container">
<div class="canvasContainer">
<img id="image" src="http://via.placeholder.com/2400x800?text=center" />
<canvas id="canvas" width="2400" height="800" />
</div>
</div>
Note: There is a text Center
present in the placeholder, but is currently not visible