--- title: CanvasRenderingContext2D.scale() slug: Web/API/CanvasRenderingContext2D/scale tags: - API - Canvas - CanvasRenderingContext2D - Method - Reference translation_of: Web/API/CanvasRenderingContext2D/scale --- <div>{{APIRef}}</div> <p>Canvas 2D APIの<code><strong>CanvasRenderingContext2D</strong></code><strong><code>.scale()</code></strong>メソッドは、キャンバス上の長さを縦方向及び横方向に拡縮する変形を適用させます。</p> <p>By default, one unit on the canvas is exactly one pixel. A scaling transformation modifies this behavior. For instance, a scaling factor of 0.5 results in a unit size of 0.5 pixels; shapes are thus drawn at half the normal size. Similarly, a scaling factor of 2.0 increases the unit size so that one unit becomes two pixels; shapes are thus drawn at twice the normal size.</p> <h2 id="構文">構文</h2> <pre class="syntaxbox notranslate">void <em>ctx</em>.scale(<em>x</em>, <em>y</em>); </pre> <h3 id="引数">引数</h3> <dl> <dt><code>x</code></dt> <dd>水平方向の拡縮係数。負の値を指定すると、縦軸を跨いでピクセルを反転させます。<code>1</code> を指定すると、水平方向には拡縮されません。</dd> <dt><code>y</code></dt> <dd>垂直方向の拡縮係数。負の値を指定すると、横軸を跨いでピクセルを反転させます。<code>1</code> を指定すると、垂直方向には拡縮されません。</dd> </dl> <h2 id="例">例</h2> <h3 id="図形を拡縮する">図形を拡縮する</h3> <p>この例は、拡縮された長方形を描きます。比較のため、元の長方形も描かれます。</p> <h4 id="HTML">HTML</h4> <pre class="brush: html notranslate"><canvas id="canvas"></canvas> </pre> <h4 id="JavaScript">JavaScript</h4> <p>The rectangle has a specified width of 8 and a height of 20. The transformation matrix scales it by 9x horizontally and by 3x vertically. 結果的に、幅は72、高さは60になります。</p> <p>キャンバス上の位置が変わることに注意してください。角の位置の指定値が(10, 10)のため、実際の角の位置は(90, 30)になります。</p> <pre class="brush: js; highlight:[5] notranslate">const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // 拡縮された長方形 ctx.scale(9, 3); ctx.fillStyle = 'red'; ctx.fillRect(10, 10, 8, 20); // 変形行列を単位行列に戻す ctx.setTransform(1, 0, 0, 1, 0, 0); // 原型の長方形 ctx.fillStyle = 'gray'; ctx.fillRect(10, 10, 8, 20); </pre> <h4 id="結果">結果</h4> <p>拡縮された<span style="color: red;">長方形は赤</span>、<span style="color: gray;">元の長方形は灰色です</span>。</p> <p>{{ EmbedLiveSample('図形を拡縮する', 700, 180) }}</p> <h3 id="垂直・水平方向の反転">垂直・水平方向の反転</h3> <p>You can use <code>scale(-1, 1)</code> to flip the context horizontally and <code>scale(1, -1)</code> to flip it vertically. In this example, the words "Hello world!" are flipped horizontally.</p> <p>Note that the call to {{domxref("CanvasRenderingContext2D.fillText()", "fillText()")}} specifies a negative x coordinate. This is to adjust for the negative scaling factor: <code>-280 * -1</code> becomes <code>280</code>, and text is drawn leftwards from that point.</p> <h4 id="HTML_2">HTML</h4> <pre class="brush: html notranslate"><canvas id="canvas"></canvas> </pre> <h4 id="JavaScript_2">JavaScript</h4> <pre class="brush: js; highlight:[4] notranslate">const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); ctx.scale(-1, 1); ctx.font = '48px serif'; ctx.fillText('Hello world!', -280, 90); ctx.setTransform(1, 0, 0, 1, 0, 0); </pre> <h4 id="結果_2">結果</h4> <p>{{ EmbedLiveSample('垂直・水平方向の反転', 700, 180) }}</p> <h2 id="仕様">仕様</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">仕様</th> <th scope="col">状況</th> <th scope="col">コメント</th> </tr> <tr> <td>{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-scale", "CanvasRenderingContext2D.scale")}}</td> <td>{{Spec2('HTML WHATWG')}}</td> <td></td> </tr> </tbody> </table> <h2 id="ブラウザの互換性">ブラウザの互換性</h2> <p>{{Compat("api.CanvasRenderingContext2D.scale")}}</p> <h2 id="参考">参考</h2> <ul> <li>このメソッドを定義するインターフェイス: {{domxref("CanvasRenderingContext2D")}}</li> </ul>