<!DOCTYPE html>
<html>
<head>
<meta lang="EN" />
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript">
// Don't resize the window
function _(str){
return document.getElementById(str);
}
function incPixel(imageData, x, y){
var index = (x + y * imageData.width) * 4;
imageData.data[index + 0] = 155;
imageData.data[index + 1] = 155;
imageData.data[index + 2] = 155;
imageData.data[index + 3] = 155;
}
$(document).ready(function(){
// collect mouse position data
var history = [];
$(document).mousemove(function(e){
history.push([e.pageX, e.pageY]);
});
// when the button is clicked
$("#button").click(function(){
// 1. disable mouse position data collection
$(document).unbind("mousemove");
// 2. draw pixels on the canvas
var width = $(document).width();
var height = $(document).height();
$("#canvas").height(height).width(width);
var canvas = document.getElementById("canvas");
if(canvas.getContext){
var context = canvas.getContext("2d");
var imageData = context.createImageData(width, height);
for(var i=0; i<history.length; i++){
incPixel(imageData, history[i][0], history[i][1]);
}
context.putImageData(imageData, 0, 0);
alert(JSON.stringify(history));
}else{
alert("no context");
}
});
});
</script>
</head>
<body>
<h1>Hello, Worlds!</h1>
<div id="width"></div>
<div id="height"></div>
<input type="button" id="button" value="Click me" /><br />
<canvas id="canvas" />
</body>
</html>
答えて
あなたはgetContext()
からコンテキスト型パラメータが欠落している、それは次のようになります。
var context = canvas.getContext('2d');
ありがとう、私は気づいた。しかし、結果はまだ私にとっては同じです。 – lowerkey
@Joshua - Chromeで動作します(デモをご覧ください)。どのブラウザで問題が発生していますか? –
@Joshua - Firefoxの場合は、キャンバス上に描かれている[既知のバグ](https://bugzilla.mozilla.org/show_bug.cgi?id=564332)です。ここでそれを回避する方法に関する質問があります:http://stackoverflow.com/questions/982000/firefox-throwing-a-exception-with-html-canvas-putimagedataata –
- 1. ルートが期待どおりに動作しないのはなぜですか?
- 2. readfが期待どおりに動作しないのはなぜですか?
- 3. jQueryが期待どおりに動作しないのはなぜですか?
- 4. TFormatSettingsが期待どおりに動作しないのはなぜですか?
- 5. dropna()が期待どおりに動作しないのはなぜですか?
- 6. mockStaticが期待どおりに動作しないのはなぜですか?
- 7. .with_lockが期待どおりに動作しないのはなぜですか?
- 8. .splitが期待どおりに動作しないのはなぜですか?
- 9. Input.GetMouseButtonUpがUnityで期待どおりに動作しない
- 10. VirtualHostがサブドメインで期待どおりに動作しない
- 11. pygameでmath.piが期待どおりに動作しない
- 12. WeakHashMapがオブザーバパターンで期待どおりに動作しない
- 13. sum()でMySQL round()が期待どおりに動作しない
- 14. UIProgressViewがNSTimerで期待どおりに動作しない - Swift
- 15. array_uniqueがPHPで期待どおりに動作しない
- 16. WPFアプリケーションでThread.Sleep()が期待どおりに動作しない
- 17. Admobプロがコルドバで期待どおりに動作しない
- 18. GreasemonkeyスクリプトがRedditで期待どおりに動作しない
- 19. MailMessage.From.DisplayNameがSmtpClient.Send()で期待どおりに動作しない
- 20. 新しいインストールでintellisenseが期待どおりに動作しないのはなぜですか?
- 21. Estimoteビーコンが期待どおりに動作していない
- 22. set/pが期待どおりに動作していない
- 23. MySQLが期待どおりに動作していない
- 24. Webdriverwaitが期待どおりに動作していない
- 25. cmd/qが期待どおりに動作していない
- 26. closest()メソッドが期待どおりに動作していない
- 27. React-routerのactiveClassNameが期待どおりに動作しない
- 28. 角4のローダーが期待どおりに動作しない
- 29. IListの比較が期待どおりに動作しない
- 30. Xcodeのオートコンプリートが期待どおりに動作しない
あなたは何を期待していますか? :) – roman