def draw_path_grid(svg, N=10, color='green', strokeWidth=0.2): W, H = svg.get_size() for i in range(N + 1): path = f'M{i*(W//N)} 0 V{H}' svg.draw(draw_path(path, stroke_width=strokeWidth, color=color)) path = f'M0 {i*(H//N)} H{W}' svg.draw(draw_path(path, stroke_width=strokeWidth, color=color))
def drawPointsLineGraphic11(svg): W, H = svg.get_size() cx, cy = W // 2, H // 2 path = f'M{W/2} 0 V{H}' svg.draw(draw_path(path, stroke_width=0.5, color='green')) path = f'M0 {H/2} H{W}' svg.draw(draw_path(path, stroke_width=0.5, color='green')) pts = get_regular_ngons(R=48, N=4) draw_plogon_subtriangle(svg, pts, (W/4, H/4)) pts = get_regular_ngons(R=48, N=5) draw_plogon_subtriangle(svg, pts, (W*3/4, H/4)) pts = get_regular_ngons(R=48, N=6) pts2 = get_regular_ngons(R=22, N=6, offset_angle=np.pi/6) pts = np.vstack((pts, pts2)) # print('pts=', pts, pts.shape) draw_plogon_subtriangle(svg, pts, (W/4, H*3/4), N=100) pts = get_regular_ngons(R=48, N=7) pts2 = get_regular_ngons(R=32, N=7, offset_angle=np.pi/7) pts = np.vstack((pts, pts2)) draw_plogon_subtriangle(svg, pts, (W*3/4, H*3/4), N=100)
def draw_path_arrow(svg): path = 'M20 10 H50 V0 L 80 20 L 50 40 V30 H20 Z' svg.draw( draw_path(path, stroke_width=0.6, color='green', fill_color='green')) path = 'M10 40 h30 v-10 l30 20 l-30 20 v-10 h-30 Z' svg.draw(draw_path(path, stroke_width=0.6, color='green', fill_color='red')) path = 'M20 70 h30 v-10 l30 20 l-30 20 v-10 h-30 Z' svg.draw( draw_path(path, stroke_width=0.6, color='green', fill_color='yellow'))
def drawPathContinuPoints(svg, pts, strokeWidth=0.5, color=None): path = 'M ' for i in range(len(pts)): x, y = pts[i] path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=strokeWidth, color=color or random_color()))
def draw_ball_movin(svg, node, radius, W, H, start_pt, step_x, step_y, N=500, color=None, draw_path_line=False): ball = BallCoordinates(x=start_pt[0], y=start_pt[1], vx=step_x, vy=step_y, width=W, height=H, offset=radius, N=N) coords = ball.get_coordinates() # print('coords=', coords, len(coords)) path = get_points_path(coords, False) # draw path line if draw_path_line: svg.draw_node(node, draw_path(path, stroke_width=0.5, color='green')) # print('path=', path[:50]) draw_circle_path_anim(svg, node, path, radius, duration=len(coords) * 2.8, color=color)
def image_svg_path2(file, dst_file): """convert raster image to svg by using path element """ image = get_binary_image(file) W = image.shape[1] H = image.shape[0] svg = SVGFileV2(dst_file, W=W, H=H, border=True) paths = get_potrace_path(image) N = 4 zoom = 1 / N for i in range(N): for j in range(N): to_point = (i * W / N, j * H / N) path = path_potrace_jagged_trans(paths, zoom_x=zoom, zoom_y=zoom, to_point=to_point) fill_color = random_color() svg.draw( draw_path(path, color='none', fill_color=fill_color, fill_rule='evenodd'))
def drawSmileSVGNode(svg, node, radius, offsetX=0, offsetY=0, color=None): x = radius + offsetX y = radius + offsetY color = color or '#FFC10E' svg.draw_node(node, draw_circle(x, y, radius, color=color)) x = radius * 0.67 + offsetX y = radius * 0.66 + offsetY r = radius * 0.16 svg.draw_node(node, draw_circle(x, y, r, color='#333333')) # left eye x = 2 * radius - x + 2 * offsetX svg.draw_node(node, draw_circle(x, y, r, color='#333333')) # right eye startPt = [0.40 * radius + offsetX, 1.05 * radius + offsetY] stopPt = [2 * radius - startPt[0] + 2 * offsetX, startPt[1]] cp1 = [0.6 * radius + offsetX, 1.78 * radius + offsetY] # Bézier Curves control points cp2 = [2 * radius - cp1[0] + 2 * offsetX, cp1[1]] # Bézier Curves control points path = 'M {} {} C {} {}, {} {}, {} {}'.format(startPt[0], startPt[1], cp1[0], cp1[1], cp2[0], cp2[1], stopPt[0], stopPt[1]) color = 'black' lineWidth = 0.11 * radius svg.draw_node(node, draw_path(path, stroke_width=lineWidth, color=color)) # mouth svg.draw_node( node, draw_circle(startPt[0], startPt[1], lineWidth / 2, color=color)) svg.draw_node( node, draw_circle(stopPt[0], stopPt[1], lineWidth / 2, color=color))
def drawOneFuncSVG(svg, ptX, ptY, N=10, color=None): x = ptX[0] y = ptY[0] path = 'M %.1f %.1f L ' % (x, y) for x, y in zip(ptX, ptY): path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=0.5, color=color or random_color()))
def draw_portrait(self, node, file, width, height, to_point=(0, 0)): image = get_binary_image(file, binary=True) W = image.shape[1] H = image.shape[0] paths = get_potrace_path(image) path = path_potrace_jagged_trans(paths, zoom_x=width/W, zoom_y=height/H, to_point=to_point) # print('len(path)=', len(path)) self.svg.draw_node(node, draw_path(path, color='none', fill_color='black', fill_rule='evenodd'))
def draw_path_star(svg): # path = 'M20 40 h60 l-50 35 l20 -55 l20 55 z' # svg.draw(draw_path(path, stroke_width=0.6, color='green', fill_color='none')) # path = 'M10 40 h80 l-65 45 l25 -75 l25 75 z' # svg.draw(draw_path(path, stroke_width=0.6, color='black', fill_color='none')) path = 'M10 40 h30 l10 -30 l10 30 h30 l-24 17 l9 28 l-25 -17 l-25 17 l9 -28 z' svg.draw(draw_path(path, stroke_width=0.6, color='red', fill_color='none'))
def drawOnePathcSVG(svg, ptX, ptY, width=1, onlyPath=True): x = ptX[0] y = ptY[0] path = 'M %.1f %.1f L ' % (x, y) for x, y in zip(ptX, ptY): path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) path = path + 'z' if onlyPath: svg.draw((path)) else: svg.draw(draw_path(path, stroke_width=width, color=random_color()))
def drawRandomWalkPath(svg): W, H = svg.get_size() times = 1000 cx, cy = W // 2, H // 2 x = cx y = cy path = 'M %.1f %.1f L ' % (x, y) for _ in range(times): x, y = random_walk(x, y, 1, step=2) path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=0.2))
def image_svg_path3(file, dst_file): """potrace to multi <path/> elements """ image = get_binary_image(file) svg = SVGFileV2(dst_file, W=image.shape[1], H=image.shape[0], border=True) paths = get_potrace_path(image) for i, path in enumerate(my_path_potrace(paths)): print(f'[{i}]', 'path=', path) svg.draw( draw_path(path, stroke_width=1.8, color='none', fill_color='#000000', fill_rule='evenodd'))
def drawRandomNumbersPath(svg): W, H = svg.get_size() times = 100 N = 500 xW = 5 for _ in range(times): path = 'M 0 0 L ' ptX = np.arange(N) + xW ptY = randomContinueNumbers(N=N) # print(ptX) # print(ptY) for x, y in zip(ptX, ptY): path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) svg.draw(draw_path(path, stroke_width=0.2, color=random_color()))
def image_svg_path(file, dst_file): """convert raster image to svg by using path element """ image = get_binary_image(file) svg = SVGFileV2(dst_file, W=image.shape[1], H=image.shape[0], border=True) paths = get_potrace_path(image) # path = path_potrace(paths) path = path_potrace_jagged(paths) print('len(path)=', len(path)) fill_color = random_color_hsv() # 'black' svg.draw( draw_path(path, stroke_width=1.8, color='none', fill_color=fill_color, fill_rule='evenodd'))
def draw_points_svg(svg, ptX, ptY, close=False, stroke=0.6, color=None, fillColor='transparent'): x = ptX[0] y = ptY[0] path = 'M %.1f %.1f L' % (x, y) for x, y in zip(ptX[1:], ptY[1:]): path = path + ' ' + str(clip_float(x)) + ' ' + str(clip_float(y)) if close: path += ' Z' # color = color or random_color_hsv() return svg.draw( draw_path(path, stroke_width=stroke, color=color, fill_color=fillColor))
def draw_basic_shapes(svg): svg.draw(sb.draw_rect(x=3, y=3, width=20, height=20, stroke_width=1, color='transparent', stroke_color='black')) rt = svg.draw(sb.draw_rect(x=28, y=3, width=20, height=20, stroke_width=1, color='transparent', stroke_color='red')) svg.set_node(rt, 'rx', '2') svg.set_node(rt, 'ry', '2') svg.draw(sb.draw_circle(x=60, y=13, radius=10, color='green')) svg.draw(sb.draw_ring(x=85, y=13, radius=8, stroke_color='red', stroke_width=3)) svg.draw(sb.draw_ellipse(cx=25, cy=35, rx=20, ry=8)) svg.draw(sb.draw_line(x=50, y=30, x2=90, y2=45, stroke_width=1, color='#23ff67')) points = '8 50 18 70 28 50 38 70 48 50 58 70 68 50 78 70 88 50 98 70' svg.draw(sb.draw_polyline(points, stroke_width=0.5, stroke_color=random_color())) points = '8 75 25 75 30 95 5 95' svg.draw(sb.draw_polygon(points, stroke_width=0.5, stroke_color=random_color())) path = 'M 35 90 Q 55 60 98 90' svg.draw(sb.draw_path(path, stroke_width=0.2, color=random_color()))
def svg_transformation(svg): """basic transforms demonstration""" W, H = svg.get_size() pts = get_5star(R=9, r=6) print('pts=', pts, pts.shape) fillColor = 'green' # random_color_hsv() stroke_w = 0.3 stroke_color = None line_color = 'green' N = 5 # lines hinter = H // N text_w = 22 start_w = (W - text_w) // 2 text_x = 1 text_y = 10 fontsize = '3px' offet_x, offet_y = 15, 11 # start to draw path = f'M{text_w} 0 V{H}' svg.draw(draw_path(path, stroke_width=stroke_w, color=line_color)) # draw grid lines for i in range(N): path = f'M0 {i * hinter} h{W}' svg.draw(draw_path(path, stroke_width=stroke_w, color=line_color)) # start translation txt = 'Translation:' svg.draw(draw_text(x=text_x, y=text_y, text=txt, fontsize=fontsize)) x, y = split_points(pts) cx = text_w + offet_x cy = offet_y px, py = translation_pts(pts, (cx, cy)) draw_points_svg(svg, px, py, stroke=None, color=stroke_color, fillColor=fillColor, close=True) cx += start_w px, py = translation_pts(pts, (cx, cy)) draw_points_svg(svg, px, py, stroke=None, color=stroke_color, fillColor=fillColor, close=True) # start rotation text_y += hinter txt = 'Rotation:' svg.draw(draw_text(x=text_x, y=text_y, text=txt, fontsize=fontsize)) cx = text_w + offet_x cy += hinter px, py = translation_pts(pts, (cx, cy)) draw_points_svg(svg, px, py, stroke=None, color=stroke_color, fillColor=fillColor, close=True) cx += start_w px, py = rotation_pts_xy(x, y, np.pi / 6) px, py = translation_pts_xy(px, py, (cx, cy)) draw_points_svg(svg, px, py, stroke=None, color=stroke_color, fillColor=fillColor, close=True) # start zoom text_y += hinter txt = 'Zoom:' svg.draw(draw_text(x=text_x, y=text_y, text=txt, fontsize=fontsize)) cx = text_w + offet_x cy += hinter px, py = translation_pts(pts, (cx, cy)) draw_points_svg(svg, px, py, stroke=None, color=stroke_color, fillColor=fillColor, close=True) cx += start_w px, py = zoom_pts_xy(x, y, 0.7) px, py = translation_pts_xy(px, py, (cx, cy)) draw_points_svg(svg, px, py, stroke=None, color=stroke_color, fillColor=fillColor, close=True) # start shear text_y += hinter txt = 'Shear:' svg.draw(draw_text(x=text_x, y=text_y, text=txt, fontsize=fontsize)) cx = text_w + offet_x cy += hinter px, py = translation_pts(pts, (cx, cy)) draw_points_svg(svg, px, py, stroke=None, color=stroke_color, fillColor=fillColor, close=True) cx += start_w px, py = shear_points(pts, r=0.7) px, py = translation_pts_xy(px, py, (cx, cy)) draw_points_svg(svg, px, py, stroke=None, color=stroke_color, fillColor=fillColor, close=True) # start reflection text_y += hinter txt = 'Reflection:' svg.draw(draw_text(x=text_x, y=text_y, text=txt, fontsize=fontsize)) cx = text_w + offet_x cy += hinter px, py = translation_pts(pts, (cx, cy)) draw_points_svg(svg, px, py, stroke=None, color=stroke_color, fillColor=fillColor, close=True) cx += start_w px, py = reflection_points(pts, reflectx=False) px, py = translation_pts_xy(px, py, (cx, cy)) draw_points_svg(svg, px, py, stroke=None, color=stroke_color, fillColor=fillColor, close=True)