このアプリhttps://developer.mozilla.org/en-US/demos/detail/meet-me-there/launchは、アップロードされた写真を受け入れ、qr コードを添付して共有できるようにします。QR 機能を除くすべての JavaScript の下に添付しました。このアプリは jQuery を使用していませんが、最初は関数を $.
window.onload = function(){
var $ = function(id){
console.log(id);
return document.getElementById(id);
},
上記の位置で console.log を使用してアプリを実行すると、かなりの数の「id」がその console.log(id) を通過していることがわかります。ファイルが読み込まれると、「surface」、「cam」、「upload」がログに記録され、アプリを使用すると、「result」、「sharer」、「uploadedURL」などのログが記録されます。問題は、console.log がコードのその時点で異なる「ID」をログに記録するために、すべてがその関数を通過し続ける方法がわからないことです。したがって、このコンテキストで「$」の意味は何なのだろうか (jQuery ではありません)。具体的には、その「$」関数を作成することで、$ を含む他のイベントが実行されるたびに呼び出されます。$('upload').onclick = function(){...
jqueryを使用$.prototype.function()
してjqueryでプロトタイプを追加するのと同じように機能しますか。もしそうなら、jQueryがない場合、どこからこの機能を取得しますか?
window.onload = function(){
var $ = function(id){
console.log(id);
return document.getElementById(id);
},
canvas = $('surface'),
ctx = canvas.getContext('2d'),
watcher, loc='No location provided', located;
$('cam').onchange = function(event){
console.log(event);
console.trace();
var files = event.target.files,
file;
if (files && files.length > 0) {
file = files[0];
try {
var URL = window.URL || window.webkitURL || window.mozURL;
var imgURL = URL.createObjectURL(file);
var img = new Image();
img.id = "tester";
//Load it onto the canvas
img.onload = function() {
console.trace();
canvas.width = this.width;
canvas.height = this.height;
$('info').innerHTML = ("Width: " + this.width + "px, Height: " + this.height + "px");
$('result').width = 400;
$('result').height = (400 / (this.width/this.height)) >> 0;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
var codeSize = (canvas.height/4) >> 0;
var imgn = new Image();
imgn.onload = function(){
ctx.drawImage(imgn, (canvas.width-5- codeSize), (canvas.height-5-codeSize), codeSize, codeSize);
$('result').src = canvas.toDataURL();
}
imgn.src = (QR.encode(loc, codeSize, 5));
}
img.src = imgURL;
} catch (e) {
console.log("error: " + e);
}
}
},
// borrowed this functionality from cgack's demo
// https://developer.mozilla.org/en-US/demos/detail/snap-and-share
$('upload').onclick = function(){
$('infomsg').style.display = 'block';
var url = "http://api.imgur.com/2/upload.json",
params = new FormData(),
http = new XMLHttpRequest();
params.append('key','29a8b1db1d8fda0df87006def2307242');
params.append('image',canvas.toDataURL().split(',')[1]);
http.open("POST", url);
http.onload = function() {
var url = JSON.parse(http.responseText).upload.links.imgur_page;
$('uploadedUrl').href = url;
$('uploadedUrl').innerHTML = url;
$('shareFb').href = ("http://www.facebook.com/sharer.php?u="+url);
$('shareTwitter').href = ("http://twitter.com/intent/tweet?url="+url);
$('sharer').style.display = 'block';
$('infomsg').style.display = 'none';
};
http.send(params);
console.log(params);
};
watcher = navigator.geolocation.watchPosition(function(position){
console.trace();
console.log("navigator");
loc = "geo:" + position.coords.latitude + "," +position.coords.longitude;
located = true;
}, function(error){
if(error.code == error.PERMISSION_DENIED || error.code == error.POSITION_UNAVAILABLE)
alert('damn, we were not able to locate you. sorry.');
}
);
};