0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

lineWidth に 0 を代入しても線は消えない

0
Posted at

線を消すつもりで ctx.lineWidth = 0 と書いても、以前の線幅が残る。Canvas は 0、負の値、Infinity、NaN の代入を無視する。

たとえば ctx.lineWidth = 6; ctx.lineWidth = 0; の後では ctx.lineWidth は 6 のままになる。その状態で strokeRect() などを呼ぶと、線を消したつもりでも枠が描かれる。

枠を表示しない設定なら、アプリ側で枠を描く処理を呼ばないようにする。幅を入力する UI では、描画前に有限の正の数か確認し、不正な値を以前の描画設定へそのまま渡さない。0 を「枠なし」として受け付けるかどうかは、アプリ側の仕様として決める。

lineWidth の初期値は 1.0 だが、不正な値を入れるたびに初期値へ戻るわけではない。代入の前後で値を読むと、どの線幅が残ったか確認できる。

私は画像プロジェクト Morphio に関わっている。このメモは Canvas API の値の扱いを説明しており、製品の実装についての説明ではない。参照:MDN lineWidth。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?