def drawBoard(self): self.newPan = doc.createElementNS("http://www.w3.org/2000/svg","svg") self.newPan.setAttribute("width",self.boardsize[0]*self.boardscale) self.newPan.setAttribute("height",self.boardsize[1]*self.boardscale) self.newPan.setAttribute("style","background-color:rgb(0,0,0);border-style:solid;border-width:1;border-color:#000;") currentDiv = doc.getElementById("div") temp = temp = "rgb("+str(255)+","+str(127)+","+str(39)+")" for x in range(self.boardsize[0]-1): line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke=temp) line.setAttribute("x1",(x+1)*self.boardscale) line.setAttribute("x2",(x+1)*self.boardscale) line.setAttribute("y1", 0 ) line.setAttribute("y2", self.boardsize[1]*self.boardscale) line.setAttribute("stroke-opacity", .17) self.newPan.appendChild( line ) for x in range(self.boardsize[1]-1): line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke=temp) line.setAttribute("x1", 0 ) line.setAttribute("x2", self.boardsize[0]*self.boardscale) line.setAttribute("y1",(x+1)*self.boardscale) line.setAttribute("y2",(x+1)*self.boardscale) line.setAttribute("stroke-opacity", .17) self.newPan.appendChild( line ) doc.body.insertBefore(self.newPan, currentDiv)
def drawBoard(self): self.newPan = doc.createElementNS("http://www.w3.org/2000/svg", "svg") self.newPan.setAttribute("width", self.boardsize[0] * self.boardscale) self.newPan.setAttribute("height", self.boardsize[1] * self.boardscale) self.newPan.setAttribute( "style", "background-color:rgb(0,0,0);border-style:solid;border-width:1;border-color:#000;" ) currentDiv = doc.getElementById("div") temp = temp = "rgb(" + str(255) + "," + str(127) + "," + str(39) + ")" for x in range(self.boardsize[0] - 1): line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke=temp) line.setAttribute("x1", (x + 1) * self.boardscale) line.setAttribute("x2", (x + 1) * self.boardscale) line.setAttribute("y1", 0) line.setAttribute("y2", self.boardsize[1] * self.boardscale) line.setAttribute("stroke-opacity", .17) self.newPan.appendChild(line) for x in range(self.boardsize[1] - 1): line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke=temp) line.setAttribute("x1", 0) line.setAttribute("x2", self.boardsize[0] * self.boardscale) line.setAttribute("y1", (x + 1) * self.boardscale) line.setAttribute("y2", (x + 1) * self.boardscale) line.setAttribute("stroke-opacity", .17) self.newPan.appendChild(line) doc.body.insertBefore(self.newPan, currentDiv)
def draw_grid(self): layer = svg.g(id='gridlayer') for y in self.row_ys: line = svg.line(x1=0, x2=self._win_width, y1=y, y2=y) line.classList.add('grid') layer <= line for x in self.col_xs: line = svg.line(x1=x, x2=x, y1=0, y2=self._win_height) line.classList.add('grid') layer <= line return layer
def drawBoard(self): self.newPan = doc.createElementNS("http://www.w3.org/2000/svg","svg") self.newPan.setAttribute("width",self.boardsize[0]*self.boardscale) self.newPan.setAttribute("height",self.boardsize[1]*self.boardscale) self.newPan.setAttribute("style","background-color:rgb(0,0,0);border-style:solid;border-width:1;border-color:#000;") currentDiv = doc.getElementById("div") for x in range(self.boardsize[0]-1): line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke="brown", stroke_width="2") line.setAttribute("x1",(x+1)*self.boardscale) line.setAttribute("x2",(x+1)*self.boardscale) line.setAttribute("y1", 0 ) line.setAttribute("y2", self.boardsize[1]*self.boardscale) line.setAttribute("stroke-opacity", .2) self.newPan.appendChild( line ) for x in range(self.boardsize[1]-1): line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke="brown", stroke_width="2") line.setAttribute("x1", 0 ) line.setAttribute("x2", self.boardsize[0]*self.boardscale) line.setAttribute("y1",(x+1)*self.boardscale) line.setAttribute("y2",(x+1)*self.boardscale) line.setAttribute("stroke-opacity", .2) self.newPan.appendChild( line ) for k in self.shiplist: line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke="green", stroke_width="5") line.setAttribute("x1", k.coord[0]*self.boardscale ) line.setAttribute("x2", (k.coord[0]+1)*self.boardscale ) line.setAttribute("y1", k.coord[1]*self.boardscale ) line.setAttribute("y2", (k.coord[1]+1)*self.boardscale ) self.newPan.appendChild( line ) line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke="green", stroke_width="5") line.setAttribute("x1", k.coord[0]*self.boardscale ) line.setAttribute("x2", (k.coord[0]+1)*self.boardscale ) line.setAttribute("y1", (k.coord[1]+1)*self.boardscale ) line.setAttribute("y2", k.coord[1]*self.boardscale ) self.newPan.appendChild( line ) doc.body.insertBefore(self.newPan, currentDiv)
def test_browser_svg(): from browser import document, svg line = svg.line(x1="40", y1="50", x2="40", y2="150", stroke="brown", stroke_width="2") panel = document['panel3'] panel <= line from browser import document, svg star = svg.polygon(fill="red", stroke="blue", stroke_width="10", points=""" 75,38 90,80 135,80 98,107 111,150 75,125 38,150 51,107 15,80 60,80""") panel = document['panel4'] panel <= star from browser import document, svg, timer rect = svg.rect(x=10, y=10, width=100, height=100) def move_rect(): # the attributes of the SVG element are strings, they must be explicitely # converted into integers rect.attrs["y"] = int(rect.attrs["y"]) + 1 # ends animation when the rectangle reaches its target if int(rect.attrs["y"]) > 50: timer.clear_interval(loop) panel = document['panel5'] panel <= rect # initialise the animation loop loop = timer.set_interval(move_rect, 30)
def render_state_svg_graphics(state): global APANEL, LINE_WIDTH, board # Clear out any graphic elements from a previous state display: # Note: This method doesn't work correctly, due to possible automatic # restructuring of part of the DOM, and subsequent failure to remove # all of the graphic elements consistently: #for idx, child in enumerate(APANEL): APANEL.removeChild(APANEL.lastChild) # The following line was used for testing: #print("Removed "+str(idx)+" elements") # The following method works fine: while APANEL.lastChild: APANEL.removeChild(APANEL.lastChild) # Draw all the boxes. for box in state['boxes']: (X1, Y1) = mapCoordsToDIV(box.x1, box.y1) (X2, Y2) = mapCoordsToDIV(box.x2, box.y2) rect_style = {"stroke":"black", "stroke-width": LINE_WIDTH} rect = svg.rect(x=X1,y=Y1, width=X2-X1, height=Y2-Y1, fill=box.color, style=rect_style) APANEL <= rect # Highlight selected box if that option is on. global SHOWING_SELECTION if SHOWING_SELECTION: selected_box = state['boxes'][state['selected']] b = selected_box (X1, Y1) = mapCoordsToDIV(b.x1, b.y1) (X2, Y2) = mapCoordsToDIV(b.x2, b.y2) selected_rect_style = {"stroke": "#8800CC", "stroke-width": LINE_WIDTH/2} selected_rect = svg.rect(x=X1,y=Y1, width=X2-X1, height=Y2-Y1, fill=b.color, style=selected_rect_style) APANEL <= selected_rect diagline_style = {"stroke": "green", "stroke-width": LINE_WIDTH} diagline = svg.line(x1=X1,y1=Y1, x2=X2, y2=Y2, style=diagline_style) APANEL <= diagline global LAST_STATE LAST_STATE = state window.makeNeededPNGs(board)
def moveAlong(self): if self.multiplayer and self.session_id != None: req = ajax.ajax() req.bind('complete', self.upknownuniverse) req.open('POST',"/upknownuniverse",True) req.set_header('content-type','application/x-www-form-urlencoded') req.send({"action":"upknownuniverse", "session_id":str(self.session_id)}) else: pass cnt = 0 for k in self.planetlist: while cnt<self.newPan.childElementCount and self.newPan.children[cnt].tagName!="circle": cnt = cnt + 1 if cnt>=self.newPan.childElementCount: crcl = svg.circle(cx=str((k.coord[0]+0.5)*self.boardscale), cy=str((k.coord[1]+0.5)*self.boardscale), r=str(self.boardscale/2)) self.newPan.appendChild( crcl ) self.newPan.children[cnt].setAttribute("cx", (k.coord[0]+0.5)*self.boardscale) self.newPan.children[cnt].setAttribute("cy", (k.coord[1]+0.5)*self.boardscale) self.newPan.children[cnt].setAttribute("r", self.boardscale/2) temp = "rgb("+str(k.ownership[0])+","+str(k.ownership[1])+","+str(k.ownership[2])+")" self.newPan.children[cnt].setAttribute("fill", temp) self.newPan.children[cnt].setAttribute("fill-opacity", 1) #print("P: ", len(self.planetlist), " ", cnt, " / ", self.newPan.childElementCount, " = ", self.newPan.children[cnt].tagName) cnt = cnt + 1 for k in range(cnt, self.newPan.childElementCount): if self.newPan.children[k].tagName=="circle": self.newPan.children[k].setAttribute("fill", "rgb(255,255,255)") self.newPan.children[k].setAttribute("fill-opacity", 0) cnt = 0 for k in self.shiplist: while cnt<self.newPan.childElementCount and self.newPan.children[cnt].tagName!="polygon": cnt = cnt + 1 isinorbit = False for m in self.planetlist: if k.coord[0] == m.coord[0] and k.coord[1] == m.coord[1]: isinorbit = True break if isinorbit: temp = str((k.coord[0]+0.5)*self.boardscale) + "," + str((k.coord[1]+(5/8))*self.boardscale) temp = temp + " " + str((k.coord[0]+0.75)*self.boardscale) + "," + str((k.coord[1]+0.5)*self.boardscale) temp = temp + " " + str((k.coord[0]+0.75)*self.boardscale) + "," + str((k.coord[1]+0.75)*self.boardscale) else: scale = 0.66 temp = str((k.coord[0]+(0.5-scale/2))*self.boardscale) + "," + str((k.coord[1]+0.5)*self.boardscale) temp = temp + " " + str((k.coord[0]+(0.5+scale/2))*self.boardscale) + "," + str((k.coord[1]+(0.5-scale/2))*self.boardscale) temp = temp + " " + str((k.coord[0]+(0.5+scale/2))*self.boardscale) + "," + str((k.coord[1]+(0.5+scale/2))*self.boardscale) if cnt>=self.newPan.childElementCount-1: trgl = svg.polygon(points=temp) self.newPan.appendChild( trgl ) #print("S: ", k.coord, " ", cnt, " / ", self.newPan.childElementCount, " = ", self.newPan.children[cnt].tagName) self.newPan.children[cnt].setAttribute("points", temp) if isinorbit: temp = "rgb(0,0,0)" else: temp = "rgb("+str(k.ownership[0])+","+str(k.ownership[1])+","+str(k.ownership[2])+")" self.newPan.children[cnt].setAttribute("fill", temp) self.newPan.children[cnt].setAttribute("fill-opacity", 1) cnt = cnt + 1 for k in range(cnt, self.newPan.childElementCount): if self.newPan.children[k].tagName=="polygon": self.newPan.children[k].setAttribute("fill", "rgb(255,255,255)") self.newPan.children[k].setAttribute("fill-opacity", 0) #print("S: ", len(self.shiplist), " ", k, " / ", self.newPan.childElementCount, " = ", self.newPan.children[k].tagName) cnt = 0 for k in range(self.boardsize[0]): for m in range(self.boardsize[1]): if self.boardcontrol[k][m] != None: for i in range(-1,2,2): if k+i>=0 and k+i<self.boardsize[0] and self.boardcontrol[k+i][m] != self.boardcontrol[k][m]: while ( cnt<self.newPan.childElementCount and ( self.newPan.children[cnt].tagName!="line" or ( abs(int(self.newPan.children[cnt].getAttribute("x1"))-int(self.newPan.children[cnt].getAttribute("x2")))==self.boardsize[0]*self.boardscale or abs(int(self.newPan.children[cnt].getAttribute("y1"))-int(self.newPan.children[cnt].getAttribute("y2")))==self.boardsize[1]*self.boardscale ) ) ): cnt = cnt + 1 if cnt>=self.newPan.childElementCount: line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke=temp) self.newPan.appendChild( line ) col = self.boardcontrol[k][m] temp = "rgb("+str(col[0])+","+str(col[1])+","+str(col[2])+")" self.newPan.children[cnt].setAttribute("stroke", temp) self.newPan.children[cnt].setAttribute("x1", int(k+0.5+0.5*i)*self.boardscale) self.newPan.children[cnt].setAttribute("x2", int(k+0.5+0.5*i)*self.boardscale) self.newPan.children[cnt].setAttribute("y1", m*self.boardscale) self.newPan.children[cnt].setAttribute("y2", (m+1)*self.boardscale) self.newPan.children[cnt].setAttribute("stroke-opacity", 1) if self.boardcontrol[k+i][m]==self.owncolorid: self.newPan.children[cnt].setAttribute("stroke-opacity", 0) cnt = cnt + 1 if m+i>=0 and m+i<self.boardsize[1] and self.boardcontrol[k][m+i] != self.boardcontrol[k][m]: while ( cnt<self.newPan.childElementCount and ( self.newPan.children[cnt].tagName!="line" or ( abs(int(self.newPan.children[cnt].getAttribute("x1"))-int(self.newPan.children[cnt].getAttribute("x2")))==self.boardsize[0]*self.boardscale or abs(int(self.newPan.children[cnt].getAttribute("y1"))-int(self.newPan.children[cnt].getAttribute("y2")))==self.boardsize[1]*self.boardscale ) ) ): cnt = cnt + 1 if cnt>=self.newPan.childElementCount: line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke=temp) self.newPan.appendChild( line ) col = self.boardcontrol[k][m] temp = "rgb("+str(col[0])+","+str(col[1])+","+str(col[2])+")" self.newPan.children[cnt].setAttribute("stroke", temp) self.newPan.children[cnt].setAttribute("x1", k*self.boardscale) self.newPan.children[cnt].setAttribute("x2", (k+1)*self.boardscale) self.newPan.children[cnt].setAttribute("y1", int(m+0.5+0.5*i)*self.boardscale) self.newPan.children[cnt].setAttribute("y2", int(m+0.5+0.5*i)*self.boardscale) self.newPan.children[cnt].setAttribute("stroke-opacity", 1) if self.boardcontrol[k][m+i]==self.owncolorid: self.newPan.children[cnt].setAttribute("stroke-opacity", 0) cnt = cnt + 1 for k in range(cnt, self.newPan.childElementCount): if ( self.newPan.children[k].tagName=="line" and ( abs(int(self.newPan.children[k].getAttribute("x1"))-int(self.newPan.children[k].getAttribute("x2")))!=self.boardsize[0]*self.boardscale or abs(int(self.newPan.children[k].getAttribute("y1"))-int(self.newPan.children[k].getAttribute("y2")))!=self.boardsize[1]*self.boardscale ) ): self.newPan.children[k].setAttribute("fill", "rgb(255,255,255)") self.newPan.children[k].setAttribute("fill-opacity", 0) for k in self.newPan.children: if k.tagName == "polygon": temp = self.newPan.removeChild( k ) self.newPan.appendChild( temp )
def moveAlong(self): if self.multiplayer and self.session_id != None: req = ajax.ajax() req.bind('complete', self.upknownuniverse) req.open('POST', "/upknownuniverse", True) req.set_header('content-type', 'application/x-www-form-urlencoded') req.send({ "action": "upknownuniverse", "session_id": str(self.session_id) }) else: pass cnt = 0 for k in self.planetlist: while cnt < self.newPan.childElementCount and self.newPan.children[ cnt].tagName != "circle": cnt = cnt + 1 if cnt >= self.newPan.childElementCount: crcl = svg.circle(cx=str((k.coord[0] + 0.5) * self.boardscale), cy=str((k.coord[1] + 0.5) * self.boardscale), r=str(self.boardscale / 2)) self.newPan.appendChild(crcl) self.newPan.children[cnt].setAttribute("cx", (k.coord[0] + 0.5) * self.boardscale) self.newPan.children[cnt].setAttribute("cy", (k.coord[1] + 0.5) * self.boardscale) self.newPan.children[cnt].setAttribute("r", self.boardscale / 2) temp = "rgb(" + str(k.ownership[0]) + "," + str( k.ownership[1]) + "," + str(k.ownership[2]) + ")" self.newPan.children[cnt].setAttribute("fill", temp) self.newPan.children[cnt].setAttribute("fill-opacity", 1) #print("P: ", len(self.planetlist), " ", cnt, " / ", self.newPan.childElementCount, " = ", self.newPan.children[cnt].tagName) cnt = cnt + 1 for k in range(cnt, self.newPan.childElementCount): if self.newPan.children[k].tagName == "circle": self.newPan.children[k].setAttribute("fill", "rgb(255,255,255)") self.newPan.children[k].setAttribute("fill-opacity", 0) cnt = 0 for k in self.shiplist: while cnt < self.newPan.childElementCount and self.newPan.children[ cnt].tagName != "polygon": cnt = cnt + 1 isinorbit = False for m in self.planetlist: if k.coord[0] == m.coord[0] and k.coord[1] == m.coord[1]: isinorbit = True break if isinorbit: temp = str((k.coord[0] + 0.5) * self.boardscale) + "," + str( (k.coord[1] + (5 / 8)) * self.boardscale) temp = temp + " " + str( (k.coord[0] + 0.75) * self.boardscale) + "," + str( (k.coord[1] + 0.5) * self.boardscale) temp = temp + " " + str( (k.coord[0] + 0.75) * self.boardscale) + "," + str( (k.coord[1] + 0.75) * self.boardscale) else: scale = 0.66 temp = str((k.coord[0] + (0.5 - scale / 2)) * self.boardscale) + "," + str( (k.coord[1] + 0.5) * self.boardscale) temp = temp + " " + str( (k.coord[0] + (0.5 + scale / 2)) * self.boardscale) + "," + str( (k.coord[1] + (0.5 - scale / 2)) * self.boardscale) temp = temp + " " + str( (k.coord[0] + (0.5 + scale / 2)) * self.boardscale) + "," + str( (k.coord[1] + (0.5 + scale / 2)) * self.boardscale) if cnt >= self.newPan.childElementCount - 1: trgl = svg.polygon(points=temp) self.newPan.appendChild(trgl) #print("S: ", k.coord, " ", cnt, " / ", self.newPan.childElementCount, " = ", self.newPan.children[cnt].tagName) self.newPan.children[cnt].setAttribute("points", temp) if isinorbit: temp = "rgb(0,0,0)" else: temp = "rgb(" + str(k.ownership[0]) + "," + str( k.ownership[1]) + "," + str(k.ownership[2]) + ")" self.newPan.children[cnt].setAttribute("fill", temp) self.newPan.children[cnt].setAttribute("fill-opacity", 1) cnt = cnt + 1 for k in range(cnt, self.newPan.childElementCount): if self.newPan.children[k].tagName == "polygon": self.newPan.children[k].setAttribute("fill", "rgb(255,255,255)") self.newPan.children[k].setAttribute("fill-opacity", 0) #print("S: ", len(self.shiplist), " ", k, " / ", self.newPan.childElementCount, " = ", self.newPan.children[k].tagName) cnt = 0 for k in range(self.boardsize[0]): for m in range(self.boardsize[1]): if self.boardcontrol[k][m] != None: for i in range(-1, 2, 2): if k + i >= 0 and k + i < self.boardsize[ 0] and self.boardcontrol[ k + i][m] != self.boardcontrol[k][m]: while (cnt < self.newPan.childElementCount and ( self.newPan.children[cnt].tagName != "line" or (abs( int(self.newPan.children[cnt].getAttribute( "x1")) - int(self.newPan.children[cnt]. getAttribute("x2"))) == self.boardsize[0] * self.boardscale or abs( int(self.newPan.children[cnt]. getAttribute("y1")) - int(self.newPan.children[cnt]. getAttribute("y2"))) == self.boardsize[1] * self.boardscale))): cnt = cnt + 1 if cnt >= self.newPan.childElementCount: line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke=temp) self.newPan.appendChild(line) col = self.boardcontrol[k][m] temp = "rgb(" + str(col[0]) + "," + str( col[1]) + "," + str(col[2]) + ")" self.newPan.children[cnt].setAttribute( "stroke", temp) self.newPan.children[cnt].setAttribute( "x1", int(k + 0.5 + 0.5 * i) * self.boardscale) self.newPan.children[cnt].setAttribute( "x2", int(k + 0.5 + 0.5 * i) * self.boardscale) self.newPan.children[cnt].setAttribute( "y1", m * self.boardscale) self.newPan.children[cnt].setAttribute( "y2", (m + 1) * self.boardscale) self.newPan.children[cnt].setAttribute( "stroke-opacity", 1) if self.boardcontrol[k + i][m] == self.owncolorid: self.newPan.children[cnt].setAttribute( "stroke-opacity", 0) cnt = cnt + 1 if m + i >= 0 and m + i < self.boardsize[ 1] and self.boardcontrol[k][ m + i] != self.boardcontrol[k][m]: while (cnt < self.newPan.childElementCount and ( self.newPan.children[cnt].tagName != "line" or (abs( int(self.newPan.children[cnt].getAttribute( "x1")) - int(self.newPan.children[cnt]. getAttribute("x2"))) == self.boardsize[0] * self.boardscale or abs( int(self.newPan.children[cnt]. getAttribute("y1")) - int(self.newPan.children[cnt]. getAttribute("y2"))) == self.boardsize[1] * self.boardscale))): cnt = cnt + 1 if cnt >= self.newPan.childElementCount: line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke=temp) self.newPan.appendChild(line) col = self.boardcontrol[k][m] temp = "rgb(" + str(col[0]) + "," + str( col[1]) + "," + str(col[2]) + ")" self.newPan.children[cnt].setAttribute( "stroke", temp) self.newPan.children[cnt].setAttribute( "x1", k * self.boardscale) self.newPan.children[cnt].setAttribute( "x2", (k + 1) * self.boardscale) self.newPan.children[cnt].setAttribute( "y1", int(m + 0.5 + 0.5 * i) * self.boardscale) self.newPan.children[cnt].setAttribute( "y2", int(m + 0.5 + 0.5 * i) * self.boardscale) self.newPan.children[cnt].setAttribute( "stroke-opacity", 1) if self.boardcontrol[k][m + i] == self.owncolorid: self.newPan.children[cnt].setAttribute( "stroke-opacity", 0) cnt = cnt + 1 for k in range(cnt, self.newPan.childElementCount): if (self.newPan.children[k].tagName == "line" and (abs( int(self.newPan.children[k].getAttribute("x1")) - int(self.newPan.children[k].getAttribute("x2"))) != self.boardsize[0] * self.boardscale or abs( int(self.newPan.children[k].getAttribute("y1")) - int(self.newPan.children[k].getAttribute("y2"))) != self.boardsize[1] * self.boardscale)): self.newPan.children[k].setAttribute("fill", "rgb(255,255,255)") self.newPan.children[k].setAttribute("fill-opacity", 0) for k in self.newPan.children: if k.tagName == "polygon": temp = self.newPan.removeChild(k) self.newPan.appendChild(temp)
def drawBoard(self): self.newPan = doc.createElementNS("http://www.w3.org/2000/svg", "svg") self.newPan.setAttribute("width", self.boardsize[0] * self.boardscale) self.newPan.setAttribute("height", self.boardsize[1] * self.boardscale) self.newPan.setAttribute( "style", "background-color:rgb(0,0,0);border-style:solid;border-width:1;border-color:#000;" ) currentDiv = doc.getElementById("div") for x in range(self.boardsize[0] - 1): line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke="brown", stroke_width="2") line.setAttribute("x1", (x + 1) * self.boardscale) line.setAttribute("x2", (x + 1) * self.boardscale) line.setAttribute("y1", 0) line.setAttribute("y2", self.boardsize[1] * self.boardscale) line.setAttribute("stroke-opacity", .2) self.newPan.appendChild(line) for x in range(self.boardsize[1] - 1): line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke="brown", stroke_width="2") line.setAttribute("x1", 0) line.setAttribute("x2", self.boardsize[0] * self.boardscale) line.setAttribute("y1", (x + 1) * self.boardscale) line.setAttribute("y2", (x + 1) * self.boardscale) line.setAttribute("stroke-opacity", .2) self.newPan.appendChild(line) for k in self.shiplist: line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke="green", stroke_width="5") line.setAttribute("x1", k.coord[0] * self.boardscale) line.setAttribute("x2", (k.coord[0] + 1) * self.boardscale) line.setAttribute("y1", k.coord[1] * self.boardscale) line.setAttribute("y2", (k.coord[1] + 1) * self.boardscale) self.newPan.appendChild(line) line = svg.line(x1="0", y1="0", x2="1", y2="1", stroke="green", stroke_width="5") line.setAttribute("x1", k.coord[0] * self.boardscale) line.setAttribute("x2", (k.coord[0] + 1) * self.boardscale) line.setAttribute("y1", (k.coord[1] + 1) * self.boardscale) line.setAttribute("y2", k.coord[1] * self.boardscale) self.newPan.appendChild(line) doc.body.insertBefore(self.newPan, currentDiv)
def generate_art(self): print("Generating art") # Figure out where we are going to put it. #output_dir = os.path.join("output", collection) #image_path = os.path.join(output_dir, f"{name}.png") # Set size parameters. rescale = 2 image_size_px = 128 * rescale padding = 12 * rescale # Create the directory and base image. # os.makedirs(output_dir, exist_ok=True) bg_color = (0, 0, 0) # image = Image.new("RGB", (image_size_px, image_size_px), bg_color) # How many lines do we want to draw? num_lines = 18 points = [] # Pick the colors. start_color = self.random_color() end_color = self.random_color(0.4) # Generate points to draw. for _ in range(num_lines): point = ( self.random_point(image_size_px, padding), self.random_point(image_size_px, padding), ) points.append(point) # Center image. # Find the bounding box. min_x = min([p[0] for p in points]) max_x = max([p[0] for p in points]) min_y = min([p[1] for p in points]) max_y = max([p[1] for p in points]) # Find offsets. x_offset = (min_x - padding) - (image_size_px - padding - max_x) y_offset = (min_y - padding) - (image_size_px - padding - max_y) # Move all points by offset. for i, point in enumerate(points): points[i] = (point[0] - x_offset // 2, point[1] - y_offset // 2) # Draw the points. current_thickness = 1 * rescale n_points = len(points) - 1 ypoints = points[1:] + [points[0]] liner = zip(points, ypoints) #alert(list(liner)) for i, ((i, j), (k, l)) in enumerate(liner): factor = i / n_points line_color = self.interpolate(start_color, end_color, factor=factor) r, g, b = *line_color style = { 'stroke': f'rgb({r},{g},{b})', 'stroke-width': current_thickness, 'stroke-linecap': "round" } lin = svg.line(x1=i, y1=j, x2=k, y2=l, style=style) self.tela <= lin # Increase the thickness. current_thickness += rescale # Image is done! Now resize it to be smooth. '''