def __init__(self): Canvas.__init__(self, 300, 300) self.sun = CanvasImage('images/sun.png') self.moon = CanvasImage('images/moon.png') self.earth = CanvasImage('images/earth.png') self.loader = ImageLoadListener() self.loader.add(self.sun) self.loader.add(self.moon) self.loader.add(self.earth) self.isActive = True self.onTimer()
class SolarCanvas(Canvas): def __init__(self): Canvas.__init__(self, 300, 300) self.sun = CanvasImage('images/sun.png') self.moon = CanvasImage('images/moon.png') self.earth = CanvasImage('images/earth.png') self.loader = ImageLoadListener() self.loader.add(self.sun) self.loader.add(self.moon) self.loader.add(self.earth) self.isActive = True self.onTimer() def onTimer(self): if not self.isActive: return Timer(100, self) self.draw() def getTimeSeconds(self): JS(""" var x = new Date(); return x.getSeconds(); """) def getTimeMilliseconds(self): JS(""" var x = new Date(); return x.getMilliseconds(); """) def draw(self): pi = 3.14159265358979323 if not self.loader.isLoaded(): return self.context.globalCompositeOperation = 'destination-over' # clear canvas self.context.clearRect(0,0,300,300) self.context.save() self.context.fillStyle = 'rgba(0,0,0,0.4)' self.context.strokeStyle = 'rgba(0,153,255,0.4)' self.context.translate(150,150) # Earth self.context.rotate( ((2*pi)/60)*self.getTimeSeconds() + ((2*pi)/60000)*self.getTimeMilliseconds() ) self.context.translate(105,0) self.context.fillRect(0,-12,50,24) # Shadow self.context.drawImage(self.earth,-12,-12) # Moon self.context.save() self.context.rotate( ((2*pi)/6)*self.getTimeSeconds() + ((2*pi)/6000)*self.getTimeMilliseconds() ) self.context.translate(0,28.5) self.context.drawImage(self.moon,-3.5,-3.5) self.context.restore() self.context.restore() self.context.beginPath() self.context.arc(150,150,105,0,pi*2,False) # Earth orbit self.context.stroke() self.context.drawImage(self.sun,0,0)
class SolarCanvas(Canvas): def __init__(self): Canvas.__init__(self, 300, 300) self.sun = CanvasImage('images/sun.png') self.moon = CanvasImage('images/moon.png') self.earth = CanvasImage('images/earth.png') self.loader = ImageLoadListener() self.loader.add(self.sun) self.loader.add(self.moon) self.loader.add(self.earth) self.isActive = True self.onTimer() def onTimer(self): if not self.isActive: return Timer(100, self) self.draw() def getTimeSeconds(self): """ var x = new Date(); return x.getSeconds(); """ def getTimeMilliseconds(self): """ var x = new Date(); return x.getMilliseconds(); """ def draw(self): if not self.loader.isLoaded(): return self.context.globalCompositeOperation = 'destination-over' # clear canvas self.context.clearRect(0, 0, 300, 300) self.context.save() self.context.fillStyle = 'rgba(0,0,0,0.4)' self.context.strokeStyle = 'rgba(0,153,255,0.4)' self.context.translate(150, 150) # Earth self.context.rotate(((2 * math.pi) / 60) * self.getTimeSeconds() + ( (2 * math.pi) / 60000) * self.getTimeMilliseconds()) self.context.translate(105, 0) self.context.fillRect(0, -12, 50, 24) # Shadow self.context.drawImage(self.earth, -12, -12) # Moon self.context.save() self.context.rotate(((2 * math.pi) / 6) * self.getTimeSeconds() + ( (2 * math.pi) / 6000) * self.getTimeMilliseconds()) self.context.translate(0, 28.5) self.context.drawImage(self.moon, -3.5, -3.5) self.context.restore() self.context.restore() self.context.beginPath() self.context.arc(150, 150, 105, 0, math.pi * 2, False) # Earth orbit self.context.stroke() self.context.drawImage(self.sun, 0, 0)