Wie eine Grenze auf HTML5 Canvas' Text hinzufügen?
-
07-07-2019 - |
Frage
Ich kann den Text mit dem Code unten ziehen,
myCanvas.fillStyle = "Red";
myCanvas.font = "30pt Arial";
myCanvas.fillText("Some Text", 10, 30);
Aber ich möchte einen Rahmen um „Some Text“ hinzuzufügen, irgendwelche Ideen dazu?
Lösung
Mit strokeText()
und der strokeStyle.
zB:
canvas = document.getElementById("myc");
context = canvas.getContext('2d');
context.fillStyle = 'red';
context.strokeStyle = 'black';
context.font = '20pt Verdana';
context.fillText('Some text', 50, 50);
context.strokeText('Some text', 50, 50);
context.fill();
context.stroke();
<canvas id="myc"></canvas>
Andere Tipps
Wir verwenden stroke Methode Rand um den Text oder Umrisses zu zeichnen, und wir verwenden können linewidth Methode die Breite des Strichs Linie zu definieren.
<script>
var canvas = document.getElementById('Canvas01');
var ctx = canvas.getContext('2d');
ctx.strokeStyle= "red"; //set the color of the stroke line
ctx.lineWidth = 3; //define the width of the stroke line
ctx.font = "italic bold 35pt Tahoma"; //set the font name and font size
ctx.strokeText("StackOverFlow",30,80); //draw the text
</script>
</body>
Was ist
myCanvas.style.border = "red 1px solid";
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow