私はjavascriptコードの小さな部分(コメントアウトされたブロックを参照)をcoffeescriptに翻訳しようとしていますが、キャンバスは空のままです。coffeescriptキャンバスが描画に失敗する
のCoffeeScript:
class CanvasDrawing
constructor: (canvasID) ->
@canvas = document.getElementById(canvasID)
@context = @canvas.getContext "2d"
drawLine: ->
y = 5/8 * @canvas.height
@context.beginPath()
@context.moveTo 0, @y
@context.lineTo @canvas.width, @y
@context.closePath()
@context.stroke()
pos =
start:
x: 0
y: @y
end:
x: @canvas.width
y: @y
window.CanvasDrawing = CanvasDrawing
HTML/Javascriptを:
<!DOCTYPE html>
<head>
<title>CanvasDrawing</title>
<script type="text/javascript" src="CanvasDrawing.js"></script>
</head>
<body>
<canvas id="canvas" style="width: 500px; height: 500px; border: 1px solid"></canvas>
<script type="text/javascript">
var canvasDrawing = new CanvasDrawing("canvas");
canvasDrawing.drawLine();
</script>
<!--
<script type="text/javascript">
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');
context.beginPath();
context.moveTo(0, 5/8 * canvas.height);
context.lineTo(canvas.width, 5/8 * canvas.height);
context.closePath();
context.stroke();
</script>
-->
</body>
</html>
かなり出力を生成しませんjavascriptのコードをコメントアウトしますが、Chromeで動作します。 私は何が欠けていますか?
クラス宣言の後のコードは、自分のコードでインデントされています。それはペーストで失われた。 – lowerkey
私はcssでキャンバスの幅と高さを設定していません。彼らは適切な 'width'と' height'属性を持っています。おそらくそれらのショットを与える? –
@herrturturインデントを修正しましたが、CoffeeScriptコードにタブとスペースが混在していると、それ自体が問題を示す可能性があります。 –