这个应用程序https://developer.mozilla.org/en-US/demos/detail/meet-me-there/launch接受上传的照片,附加一个二维码,然后允许你分享它们。除了 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.');
}
);
};