HTML-canvas strokeText() Methode
Voorbeeld
Schrijf "Hallo wereld!" en "Grote glimlach!" (met verloop) op het canvas, met strokeText():
javascript:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "20px Georgia";
ctx.strokeText("Hello World!", 10, 50);
ctx.font = "30px Verdana";
// Create gradient
var gradient = ctx.createLinearGradient(0, 0, c.width, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("1.0", "red");
// Fill with gradient
ctx.strokeStyle = gradient;
ctx.strokeText("Big smile!", 10, 90);
Browserondersteuning
De getallen in de tabel geven de eerste browserversie aan die de methode volledig ondersteunt.
Method | |||||
---|---|---|---|---|---|
strokeText() | 4.0 | 9.0 | 3.6 | 4.0 | 10.1 |
Opmerking: de parameter maxWidth wordt niet ondersteund in Safari.
Definitie en gebruik
De methode strokeText() tekent tekst (zonder vulling) op het canvas. De standaardkleur van de tekst is zwart.
Tip: gebruik de eigenschap font om het lettertype en de tekengrootte op te geven en gebruik de eigenschap strokeStyle om de tekst in een andere kleur/verloop weer te geven.
JavaScript-syntaxis: | context .strokeText ( tekst, x, y, maxWidth ); |
---|
Parameterwaarden
Parameter | Description | Play it |
---|---|---|
text | Specifies the text that will be written on the canvas | |
x | The x coordinate where to start painting the text (relative to the canvas) | |
y | The y coordinate where to start painting the text (relative to the canvas) | |
maxWidth | Optional. The maximum allowed width of the text, in pixels |
❮ Canvasobject