这是示例中javascript的网址:https : //github.com/madbence/node-drawille/blob/master/examples/clock.js
这是示例脚本的全文
var Canvas = require('../');
var line = require('bresenham');
var c = new Canvas(160, 160);
function draw() {
c.clear();
var t = new Date();
var sin = function(i, l) {
return Math.floor(Math.sin(i*2*Math.PI)*l+80);
}, cos = function(i, l) {
return Math.floor(Math.cos(i*2*Math.PI)*l+80);
};
line(80, 80, sin(t.getHours()/24, 30), 160-cos(t.getHours()/24, 30), c.set.bind(c));
line(80, 80, sin(t.getMinutes()/60, 50), 160-cos(t.getMinutes()/60, 50), c.set.bind(c));
line(80, 80, sin(t.getSeconds()/60+(+t%1000)/60000, 75), 160-cos(t.getSeconds()/60+(+t%1000)/60000, 75), c.set.bind(c));
process.stdout.write(c.frame());
}
setInterval(draw, 1000/24);
我不太明白为什么c.set.bind(c)
在line
函数调用中这样
因为set
是一个功能。您正在传递Function
对象作为参数。如果set
内部使用函数,this
则this
运行时的值取决于调用对象。要使其正确引用this
而不管调用对象是什么,您都需要使用bind
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句