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, t=None): if not self.isActive: return Timer(100, self) self.draw() def getTimeSeconds(self): return time.time() % 60 def getTimeMilliseconds(self): return (time.time() * 1000.0) % 1.0 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.getElement(), -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.getElement(), -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.getElement(), 0, 0)
class ClockCanvas(Canvas): def __init__(self, img_url): Canvas.__init__(self, 200, 200) self.img_url = img_url self.clock = CanvasImage(self.img_url) self.width = 200 self.height = 200 self.plusGMT = 0 #Time would be GMT+plusGMT self.loader = ImageLoadListener(self) self.loader.add(self.clock) self.isActive = True self.onTimer() def onError(self, sender): Window.alert("error of some kind (probably missing image at url)") def onTimer(self, sender=None): if not self.isActive: return Timer(1000, self) self.draw() def getTimeSeconds(self): return time.time() % 60.0 def getTimeMilliseconds(self): return (time.time() * 1000.0) % 1.0 def getTimeMinutes(self): return (time.time() / 60) % 60.0 def getTimeHours(self): return ((time.time() + self.plusGMT * 3600) / 3600) % 12.0 def draw(self): pi = 3.14159265358979323 if not self.loader.isLoaded(): return self.context.globalCompositeOperation = 'destination-over' # clear canvas self.context.clearRect(0,0,self.width,self.height) self.context.save() self.context.fillStyle = 'rgba(0,0,0,0.4)' self.context.strokeStyle = 'rgba(0,153,255,0.4)' self.context.translate(self.width/2,self.height/2) secs = self.getTimeSeconds() mins = self.getTimeMinutes() hours = self.getTimeHours() # Seconds self.context.save() self.context.fillStyle = 'rgba(255,0,0,0.4)' self.context.rotate( ((2*pi)/60)*secs + pi) self.context.fillRect(-1,-(self.width * 0.04),2, self.width * 0.38) self.context.restore() # Minutes self.context.save() self.context.rotate( ((2*pi)/60)*mins + pi) self.context.fillRect(-1,-1,3,self.width * 0.35) self.context.restore() # Hours self.context.save() self.context.rotate( ((2*pi)/12)*hours + pi) self.context.fillRect(-2,-2,4,self.width * 0.2) self.context.restore() self.context.restore() self.context.drawImage(self.clock.getElement(),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, t=None): if not self.isActive: return Timer(100, self) self.draw() def getTimeSeconds(self): return time.time() % 60 def getTimeMilliseconds(self): return (time.time() * 1000.0) % 1.0 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.getElement() ,-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.getElement(),-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.getElement(),0,0)
class SolarCanvas(Canvas): def __init__(self, img_url): Canvas.__init__(self, 300, 300) self.clock = CanvasImage(img_url) self.width = 150 self.height = 150 self.loader = ImageLoadListener(self) self.loader.add(self.clock) self.isActive = True self.onTimer() def onLoad(self, sender=None): el = self.clock.getElement() self.width = DOM.getIntAttribute(el, "width") self.height = DOM.getIntAttribute(el, "height") self.setWidth("%dpx" % self.width) self.setHeight("%dpx" % self.height) def onError(self, sender): Window.alert("error of some kind (probably missing image at url)") def onTimer(self, sender=None): if not self.isActive: return Timer(1000, self) self.draw() def getTimeSeconds(self): return time.time() % 60.0 def getTimeMilliseconds(self): return (time.time() * 1000.0) % 1.0 def getTimeMinutes(self): return (time.time() / 60) % 60.0 def getTimeHours(self): return (time.time() / 3600) % 12.0 def draw(self): pi = 3.14159265358979323 if not self.loader.isLoaded(): return self.context.globalCompositeOperation = 'destination-over' # clear canvas self.context.clearRect(0,0,self.width,self.height) self.context.save() self.context.fillStyle = 'rgba(0,0,0,0.4)' self.context.strokeStyle = 'rgba(0,153,255,0.4)' self.context.translate(self.width/2,self.height/2) secs = self.getTimeSeconds() mins = self.getTimeMinutes() + secs / 60.0 hours = self.getTimeHours() + mins / 60.0 # Seconds self.context.save() self.context.fillStyle = 'rgba(255,0,0,0.4)' self.context.rotate( ((2*pi)/60)*secs + pi) self.context.fillRect(-1,-(self.width * 0.04),2, self.width * 0.38) self.context.restore() # Minutes self.context.save() self.context.rotate( ((2*pi)/60)*mins + pi) self.context.fillRect(-1,-1,3,self.width * 0.35) self.context.restore() # Hours self.context.save() self.context.rotate( ((2*pi)/12)*hours + pi) self.context.fillRect(-2,-2,4,self.width * 0.2) self.context.restore() self.context.restore() self.context.drawImage(self.clock.getElement(),0,0)