Package pyjamas :: Module Canvas2D
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.Canvas2D

  1  # Canvas wrapper component for Pyjamas 
  2  # Ported by Willie Gollino from Canvas component for GWT - Originally by Alexei Sokolov http://gwt.components.googlepages.com/ 
  3  # 
  4  # Canvas API reference: 
  5  # http://developer.apple.com/documentation/AppleApplications/Reference/SafariJSRef/Classes/Canvas.html#//apple_ref/js/Canvas.clearRect 
  6  # 
  7  # Usage Notes: 
  8  #   - IE support requires ExplorerCanvas from excanvas.sourceforge.net 
  9  #   - place excanvas.js in your apps public folder 
 10  #   - add this to your MainModule.html: <!--[if IE]><script src="excanvas.js" type="text/javascript"></script><![endif]--> 
 11   
 12  from pyjamas import DOM 
 13  from pyjamas.ui.Image import Image 
 14  from pyjamas.ui.FocusWidget import FocusWidget 
 15  from pyjamas.ui import Event 
 16  from pyjamas.ui import MouseListener 
 17  from pyjamas.ui import KeyboardListener 
 18  from pyjamas.ui import Focus 
 19  from pyjamas.ui import FocusListener 
 20   
 21  from __pyjamas__ import JS 
 22   
23 -class Canvas(FocusWidget):
24 - def __init__(self, Width=0, Height=0, **kwargs):
25 if not kwargs.has_key('StyleName'): 26 kwargs['StyleName'] = 'gwt-Canvas' 27 kwargs['Width'] = Width 28 kwargs['Height'] = Height 29 30 self.context = None 31 32 focusable = Focus.createFocusable() 33 self.canvas = DOM.createElement("canvas") 34 DOM.appendChild(focusable, self.canvas) 35 FocusWidget.__init__(self, focusable, **kwargs) 36 37 self.init() 38 39 self.context.fillStyle = "black" 40 self.context.strokeStyle = "black"
41
42 - def setWidth(self, width):
43 FocusWidget.setWidth(self, width) 44 self.canvas.width = width
45
46 - def setHeight(self, height):
47 FocusWidget.setHeight(self, height) 48 self.canvas.height = height
49
50 - def getContext(self):
51 return self.context
52
53 - def isEmulation(self):
54 return False
55
56 - def init(self):
57 el = self.getElement().firstChild 58 ctx = el.getContext("2d") 59 60 """ 61 ctx._createPattern = ctx.createPattern 62 ctx.createPattern = function(img, rep) { 63 if (!(img instanceof Image)) img = img.getElement(); 64 return self._createPattern(img, rep) 65 } 66 67 ctx._drawImage = ctx.drawImage 68 ctx.drawImage = function() { 69 var a=arguments 70 if (!(a[0] instanceof Image)) a[0] = a[0].getElement() 71 if (a.length==9) return self._drawImage(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]) 72 else if (a.length==5) return self._drawImage(a[0], a[1], a[2], a[3], a[4]) 73 return self._drawImage(a[0], a[1], a[2]) 74 } 75 """ 76 self.context = ctx
77
78 -class CanvasImage(Image):
79 - def __init__(self, url="", load_listener = None):
80 Image.__init__(self, url) 81 if load_listener: 82 self.addLoadListener(load_listener) 83 self.onAttach()
84
85 - def isLoaded(self):
86 return self.getElement().complete
87 88
89 -class ImageLoadListener:
90 - def __init__(self, listener = None):
91 self.wait_list = [] 92 self.loadListeners = [] 93 94 if listener: 95 self.addLoadListener(listener)
96
97 - def add(self, sender):
98 self.wait_list.append(sender) 99 sender.addLoadListener(self)
100
101 - def addLoadListener(self, listener):
102 self.loadListeners.append(listener)
103
104 - def isLoaded(self):
105 if len(self.wait_list): 106 return False 107 return True
108
109 - def onError(self, sender):
110 for listener in self.loadListeners: 111 listener.onError(sender)
112
113 - def onLoad(self, sender):
114 self.wait_list.remove(sender) 115 116 if self.isLoaded(): 117 for listener in self.loadListeners: 118 listener.onLoad(self)
119