def fold(startX, startY, height, currentPage): colorOffset = int(startX / canvasWidth * 300) horizontalColorOffset = startX / canvasWidth verticalColorOffset = startY / canvasHeight color = (220 - horizontalColorOffset * 50, 30 + horizontalColorOffset * 30, 90 + horizontalColorOffset * 40) color = breathingColor(currentPage, *color) gradientSteps = 80 for x in range(0, gradientSteps): colorOffset -= colorOffset + x / gradientSteps * 50 # if(x % 2 == 0): # db.fill(*rgb(color[0]+ colorOffset, color[1] + colorOffset, color[2] + colorOffset)) # else: # db.fill(1,1,1) db.fill(*rgb(color[0] + colorOffset, color[1] + colorOffset, color[2] + colorOffset)) db.polygon( (startX + ((height * 0.3) * x / gradientSteps), startY + height * 0.3 * x / gradientSteps), (startX + height * 2 - ((height * 0.2) * x / gradientSteps), startY + ((height * 0.3) * x / gradientSteps)), (startX + height * 1.5, startY + height * 0.8), (startX - height / 2 + (height * 0.3 * x / gradientSteps), startY + height - 0.1 * (height * 0.3) * x / gradientSteps), )
def draw_sierpinski_r(x_1, y_1, x_2, y_2, x_3, y_3, limit=6): draw.fill(random(), random(), random(), 1) draw.polygon((x_1, y_1), (x_2, y_2), (x_3, y_3)) side = (x_2 - x_1) / 2 desp = sqrt(3 / 4) * side if limit > 0: draw_sierpinski_r(x_1 - side / 2, y_1 - desp, x_1 + side / 2, y_1 - desp, x_1, y_3, limit - 1) draw_sierpinski_r(x_1 + side / 2, y_1 + desp, x_2 - side / 2, y_2 + desp, x_1 + side, y_1, limit - 1) draw_sierpinski_r(x_3 + side / 2, y_3 + desp, x_2 + side / 2, y_2 - desp, x_3 + side, y_3, limit - 1)
def test_polygon_unexpectedKeywordArgument(self): drawBot.newDrawing() drawBot.polygon((1, 2), (3, 4), close=True) drawBot.polygon((1, 2), (3, 4), close=False) with self.assertRaises(TypeError): drawBot.polygon((1, 2), (3, 4), closed=False) with self.assertRaises(TypeError): drawBot.polygon((1, 2), (3, 4), closed=False, foo=123)
def test_polygon_notEnoughPoints(self): drawBot.newDrawing() with self.assertRaises(TypeError): drawBot.polygon() with self.assertRaises(TypeError): drawBot.polygon((1, 2)) drawBot.polygon((1, 2), (3, 4))
def draw_square(p1: tuple, p2: tuple): draw.fill(random(), random(), random(), 1) corner_1, corner_2 = (p1[0], p2[1]), (p2[0], p1[1]) draw.polygon(p1, corner_1, p2, corner_2)
# Licensed under MIT conditions # import sys import drawBot if __name__ == "__main__": sys.path.insert(0, "..") # So we can import pagebotnano003 without installing. from pagebotnano_000 import export W = H = 600 M = 50 INSET = 150 # Create a new page canvas of 1000 x 1000 px drawBot.newPage(W, H) # Fill page with white background drawBot.fill(1) drawBot.rect(0, 0, W, H) # Set fill color to red (r, g, b) drawBot.fill(0.2, 0.2, 1) # Draw a polygon with x-amount of points drawBot.polygon( (M, M), (M+INSET, W/2), (M, H-M), # Left (W/2, H-M-INSET), # Top (W-M, H-M), (W-M-INSET, H/2), (W-M, M), # Right (W/2, INSET), # Bottom close=True) # Export as png file in created _export folder (that does not sync in Github) export('_export/0020-PolygonPar.png')
import drawBot drawBot.size(200, 200) drawBot.stroke(0) drawBot.strokeWidth(10) drawBot.fill(1, 0.3, 0) drawBot.polygon((40, 40), (40, 160)) drawBot.polygon((60, 40), (60, 160), (130, 160)) drawBot.polygon((100, 40), (160, 160), (160, 40), close=False)
def new(layer, totalPages): d.newPage(paperSize) w, h = d.width(), d.height() m = l.associatedFontMaster() d.font(".SF Compact Text", 10) d.text("%s %s" % (layer.parent.name, layer.name), (margin, margin)) d.text("%s/%s" % (d.pageCount(), totalPages - 1), (w - margin, margin), align="right") ma, md, mx, mc = m.ascender, m.descender, m.xHeight, m.capHeight zones = [az.position + az.size for az in m.alignmentZones] + [ma, md] boundsTop, boundsBtm = max(zones), min(zones) sf = float(h - margin * 3) / (boundsTop - boundsBtm) #scalefactor d.scale(sf) wNew = w / sf # scaled paper size d.translate((margin / sf), -boundsBtm + (margin * 2) / sf) # drawing metrics lines d.stroke(0, 0, 0, 0.5) d.strokeWidth(0.5 / sf) d.fill(None) lw = layer.width d.rect(0, md, lw, ma - md) d.line((0, mc), (lw, mc)) # x-height d.line((0, mx), (lw, mx)) # x-height d.line((0, 0), (lw, 0)) # baseline # alignment zones d.stroke(None) d.fill(0.7, 0.3, 0, 0.1) for az in m.alignmentZones: d.rect(0, az.position, lw, az.size) # drawing nodes offcurves = [] smooths = [] sharps = [] for p in layer.paths: smooths += [n for n in p.nodes if n.connection == GSSMOOTH] sharps += [ n for n in p.nodes if n.type != OFFCURVE and n.connection != GSSMOOTH ] offcurves += [n for n in p.nodes if n.type == OFFCURVE] d.stroke(0, 0, 0, 0.2) for n in p.nodes: if n.type == OFFCURVE: if n.nextNode.type != OFFCURVE: d.line((n.x, n.y), (n.nextNode.x, n.nextNode.y)) elif n.prevNode.type != OFFCURVE: d.line((n.prevNode.x, n.prevNode.y), (n.x, n.y)) d.stroke(None) nodeSize = 3 / sf hf = nodeSize / 2 #half d.fill(0, 0, 1, 0.5) for n in sharps: d.rect(n.x - hf, n.y - hf, nodeSize, nodeSize) d.fill(0, 0.7, 0, 0.5) for n in smooths: d.oval(n.x - hf, n.y - hf, nodeSize, nodeSize) d.fill(0, 0, 0, 0.2) for n in offcurves: d.oval(n.x - hf, n.y - hf, nodeSize, nodeSize) # drawing anchors d.stroke(None) d.fill(0.7, 0.25, 0.0, 0.75) nodeSize = 4 / sf hf = nodeSize * 0.7 for a in layer.anchors: # print(a, (ma, md, mc, mx, 0)) if a.y in (ma, md, mc, mx, 0): d.polygon((a.x - hf, a.y), (a.x, a.y - hf), (a.x + hf, a.y), (a.x, a.y + hf), close=True) else: d.oval(a.x - hf, a.y - hf, nodeSize, nodeSize) # glyph outline d.fill(0, 0, 0, 0.3) d.stroke(0, 0, 0, 1) d.drawPath(layer.completeBezierPath) return sf, ma, md
import drawBot if __name__ == "__main__": sys.path.insert( 0, "..") # So we can import pagebotnano003 without installing. from pagebotnano_000 import export W = H = 600 # Create a new page canvas of 1000 x 1000 px drawBot.newPage(600, 600) # Fill page with white background drawBot.fill(1) drawBot.rect(0, 0, W, H) # Set fill color to red (r, g, b) drawBot.fill(0.2, 1, 1) # Draw a black square (x, y, width, height) # draw a polygon with x-amount of points drawBot.polygon( (50, 50), (100, 300), (50, 550), # Left (300, 500), # Top (550, 550), (500, 300), (550, 50), # Right (300, 100), # Bottom close=True) # Export as png file in created _export folder (that does not sync in Github) export('_export/0020-Polygon.png')
pt1 = 238, 182 pt2 = 46, 252 radius = 60 def drawPt(pos, r=5): x, y = pos drawBot.oval(x-r, y-r, r*2, r*2) drawBot.fill(None) path = drawBot.BezierPath() path.moveTo(pt0) path.arcTo(pt1, pt2, radius) path.lineTo(pt2) drawBot.stroke(0, 1, 1) drawBot.polygon(pt0, pt1, pt2) for pt in [pt0, pt1, pt2]: drawPt(pt) drawBot.stroke(0, 0, 1) drawBot.drawPath(path) drawBot.stroke(1, 0, 1) for pt in path.onCurvePoints: drawPt(pt, r=3) for pt in path.offCurvePoints: drawPt(pt, r=2) # Export as png file in created _export folder (that does not sync in Github) export('_export/0030-Arcs.png')