What is the relationship between canvas scale and canvas size?
<p>I have 240x157 png, and the div size is 120x88</p><p>So, I made a 240x157 canvas and pasted the png. </p><p>The size of the div is smaller than that of the png, but the div has a scroll bar, and I can use the drag bar to see the entire image. This is perfect. </p><p><code></code><code></code></p>
<p><br /></p>
<pre class="brush:js;toolbar:false;">var ctx = $('canvas')[0].getContext('2d');
console.log("test")
var chara = new Image();
chara.src = "https://dl.dropbox.com/s/yr8ehzbdwm0csc7/Madeira_-_Entrance_to_Town,_c._1900.jpg?dl=0";
chara.onload = () => {
ctx.clearRect(0, 0,240,157);
ctx.drawImage(chara,0,0,240,157);
}</pre>
<pre class="brush:css;toolbar:false;">#whole{
border:1px solid;
width:120px;
height:78px;
overflow:scroll;
}</pre>
<pre class="brush:html;toolbar:false;"><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> ;</script>
<div id="whole">
<canvas id="test" width="240px" height="157px"></canvas>
</div></pre>
<p><br /></p>
<p>My ultimate goal is to change the proportion by the user, and when the image is larger than the div, a scroll bar will appear, like photoshop, etc. </p><p>So, I tested the 1/2 scale and saw that the whole image looked successful at 129 x78</p> Area. </p><p>Why is this happening?</p><p>In my understanding, when the canvas size is 240x157 and the ratio is 1/2, 120x78 should appear. Why is only the picture shrinking? Rather than the canvas itself?</p><p><code></code><code></code></p>
<p><br /></p>
<pre class="brush:js;toolbar:false;">var ctx = $('canvas')[0].getContext('2d');
console.log("test")
var chara = new Image();
chara.src = "https://dl.dropbox.com/s/yr8ehzbdwm0csc7/Madeira_-_Entrance_to_Town,_c._1900.jpg?dl=0";
ctx.scale(1/2,1/2) // adding here.
chara.onload = () => {
ctx.clearRect(0, 0,240,157);
ctx.drawImage(chara,0,0,240,157);
}</pre>
<pre class="brush:css;toolbar:false;">#whole{
border:1px solid;
width:120px;
height:78px;
overflow:scroll;
}</pre>
<pre class="brush:html;toolbar:false;"><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> ;</script>
<div id="whole">
<canvas id="test" width="240px" height="157px"></canvas>
</div></pre>
<p><br /></p>