0
以下の例では、json.mesがAjaxからの受信メッセージです。キャンバスボックスにメッセージを表示するボタンがデザインされています。私はgetElementByIdを使ってメッセージを読み込もうとしました(下記参照)。ただし、動作しません。どんな解決策ですか?Javascriptを使用してキャンバスオブジェクトのテキストを変更します
<div> <button id="submit" type="button" class="btn btn-success" style="float: right;">Populate</button></div>
<script>
var canvas = document.getElementById("myCanvas3");
var ctx = canvas.getContext("2d");
ctx.fillRect(0, 0, 150, 150);
ctx.fillStyle = "#dbbd7a";
ctx.font = "10px Comic Sans MS";
ctx.fillStyle = "red";
ctx.textAlign = "center";
ctx.fillStyle = "#ff0000";
ctx.fillText(document.getElementById("picoutput0"), 74, 74);
</script>
<script>
...
$("#submit").click(function(e) {
...
var text0=json.mes
$('#picoutput0').html(text0);
</script>
'のdocument.getElementById( "picoutput0")は、このコードを試してみてください、しかし、あなたは自分に' text0'を利用できるようにする方が良いと思いますキャンバススクリプト(現在はすでにグローバルスコープにあるようです)を直接呼び出して、 'ctx.fillText(text0、x、y)'を直接呼び出します。 – Kaiido
@Kaiido私はそれを試しました。それは動作しません。 .innerHTMLも動作しません。 – salehinejad
この質問の可能な複製http://stackoverflow.com/questions/31882814/html-canvas-change-text-dynamically – subbu