TomFree
TomFree

Reputation: 1409

Setting context.canvas.width changes context.font

I want to create an image via a canvas. However some weird stuff happens as context.font is changed when I assign context.canvas.width a value.

Here is my code:

var canvas = window.document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.fillStyle = "#0000ff";
context.font = "32px Arial";
console.log(context.font); // 32px Arial
var mT = context.measureText("Hello World!");
console.log(context.font); // 32px Arial
var width = mT.width;
console.log(context.font); // 32px Arial
context.canvas.width = width;
console.log(context.font); // 10px sans-serif

Upvotes: 0

Views: 63

Answers (1)

ryachza
ryachza

Reputation: 4540

When you change the size of the canvas, every property of the context is reset to its default value.

Upvotes: 1

Related Questions