def on_enter_frame(self, scene, context): g = graphics.Graphics(context) self.two_tile_random(context) g.move_to(0,0) g.show_label("Hello", size=48, color="#33a") # creating a in-memory image of current context image_surface = cairo.ImageSurface(cairo.FORMAT_RGB24, self.width, self.height) image_context = cairo.Context(image_surface) # copying image_context.set_source_surface(context.get_target()) image_context.paint() # buffer allows us to manipulate the pixels directly buffer = image_surface.get_data() # blur self.blur(buffer) # and paint it back context.set_source_surface(image_surface) context.paint() self.redraw()
def on_enter_frame(self, scene, context): self.start_angle += 0.01 * 60 / self.framerate # good way to keep the speed constant when overriding frame rate g = graphics.Graphics(context) g.fill_area(0, 0, self.width, self.height, "#f00") g.set_line_style(width=0.5) g.set_color("#fff") x, y = self.width / 2, self.height / 2 x = x + math.sin(self.start_angle * 0.3) * self.width / 4 center_distance = math.cos(self.start_angle) * self.width / 8 angle = self.start_angle step = math.pi * 2 / 64 distance = max(self.width, self.height) while angle < self.start_angle + math.pi * 2: g.move_to(x + math.cos(angle) * center_distance, y + math.sin(angle) * center_distance) g.line_to(x + math.cos(angle) * distance, y + math.sin(angle) * distance) g.line_to(x + math.cos(angle + step) * distance, y + math.sin(angle + step) * distance) g.line_to(x + math.cos(angle) * center_distance, y + math.sin(angle) * center_distance) angle += step * 2 g.fill() self.redraw()
def on_finish_frame(self, scene, context): if self.mouse_node and self.mouse: c_graphics = graphics.Graphics(context) c_graphics.set_color("#666") c_graphics.move_to(self.mouse_node.x, self.mouse_node.y) c_graphics.line_to(*self.mouse) c_graphics.stroke()
def on_enter_frame(self, scene, context): if not self.facts: return g = graphics.Graphics(context) g.set_line_style(width=1) start_date = self.facts[0].start_time.date() end_date = (self.facts[-1].start_time + self.facts[-1].delta).date() days = (end_date - start_date).days full_days = [] for day in range(days): current_date = start_date + dt.timedelta(days=day) if not self.day_counts[current_date]: continue full_days.append(self.day_counts[current_date]) day_pixel = float(self.width) / len(full_days) cur_x = 0 pixel_width = max(round(day_pixel), 1) for day in full_days: cur_x += round(day_pixel) for j, fact in enumerate(day): #bar per category g.rectangle(cur_x, 27 + self.categories.index(fact.category) * 6, pixel_width, 6) #bar per activity g.rectangle(cur_x, 102 + self.activities.index(fact.activity) * 6, pixel_width, 6) #number of activities g.rectangle(cur_x, self.height - 3 * j, pixel_width, 3) g.fill("#aaa")
def on_enter_frame(self, scene, context): # you could do all your drawing here, or you could add some sprites g = graphics.Graphics(context) g.translate(self.width / 2, self.height / 2 - 100) g.move_to(-500, 0) g.line_to(500, 0) g.stroke("#33F2F0", 0.2) for z in range(-10, 1000, 50): for dot in (self.a, self.b): dot.z = z - self.viewport_depth g.move_to(*self.a) g.line_to(*self.b) #g.set_line_style(int(30 * (1 - z / 1000.0))) g.stroke("#33F2F0", 1 - z / 1000.0 * 0.8) self.viewport_depth += 1 if self.viewport_depth > 50: self.viewport_depth = 0 self.redraw()
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) for i in range(self.total_hours): hour = self._date + dt.timedelta(hours=i) y = delta_minutes(self._date, hour) * self.pixels_in_minute g.move_to(0, self.fact_list.y + y) g.show_label(hour.strftime("%H:%M"), 10, "#666")
def on_enter_frame(self, scene, context): c_graphics = graphics.Graphics(context) if len(self.flock) < 80: for i in range(2): self.flock.append( Boid(Vector2(self.width / 2, self.height / 2), 2.0, 0.05)) # main loop (i should rename this to something more obvious) c_graphics.set_line_style(width=0.8) c_graphics.set_color("#666") for boid in self.flock: neighbours = [] if self.frame % 2 == 0: #recalculate direction every second frame neighbours = self.proximities.find_neighbours(boid, 40) boid.run(neighbours) self.wrap(boid) self.proximities.update_position(boid) self.draw_boid(context, boid) self.frame += 1 context.stroke() self.redraw()
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) g.fill_area(0, 0, self.width, self.height, "#20b6de") self.chart.x = self.width / 2 self.chart.y = self.height / 2 self.chart.scale_x = 0.18 self.chart.scale_y = 0.18
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) g.rectangle(50, 50, 50, 200) g.fill("#f00", 0.5) g.rectangle(50, 100, 50, 200) g.fill("#0f0", 0.5) self.redraw()
def on_enter_frame(self, scene, context): c_graphics = graphics.Graphics(context) if not self.graph: self.new_graph() self.graph.update(self.width, self.height) if self.redo_layout: self.redo_layout = False self.graph.init_layout(self.width, self.height) # first draw c_graphics.set_line_style(width=0.5) done = abs(self.graph.minimum_temperature - self.graph.temperature) < 0.05 if not done: c_graphics.set_color("#aaa") else: c_graphics.set_color("#666") for edge in self.edge_buffer: context.move_to(edge[0].x, edge[0].y) context.line_to(edge[1].x, edge[1].y) context.stroke() if not done: # then recalculate positions self.graph.update(self.width, self.height) # find bounds min_x, min_y, max_x, max_y = self.graph.graph_bounds graph_w, graph_h = max_x - min_x, max_y - min_y factor_x = self.width / float(graph_w) factor_y = self.height / float(graph_h) graph_mid_x = (min_x + max_x) / 2.0 graph_mid_y = (min_y + max_y) / 2.0 mid_x, mid_y = self.width / 2.0, self.height / 2.0 factor = min( factor_x, factor_y ) * 0.9 # just have the smaller scale, avoid deformations for i, node in enumerate(self.display_nodes): self.tweener.kill_tweens(node) self.animate( node, x=mid_x + (self.graph.nodes[i].x - graph_mid_x) * factor, y=mid_y + (self.graph.nodes[i].y - graph_mid_y) * factor, easing=Easing.Expo.ease_out, duration=3) self.redraw()
def on_enter_frame(self, scene, context): c_graphics = graphics.Graphics(context) c_graphics.set_line_style(width = 0.8) for waypoint in self.waypoints: waypoint.update(context) for boid in self.boids: # the growing antennae circle if self.debug_radius: c_graphics.set_color("#aaa", 0.3) context.arc(boid.location.x, boid.location.y, boid.radio, -math.pi, math.pi) context.fill() # obstacle awareness circle if self.debug_awareness: c_graphics.set_color("#aaa", 0.5) context.arc(boid.location.x, boid.location.y, boid.awareness, -math.pi, math.pi) context.fill() for boid in self.boids: neighbours = self.proximities.find_neighbours(boid, 40) boid.run(neighbours) self.proximities.update_position(boid) # debug trail (if enabled) c_graphics.set_color("#0f0") for position1, position2 in zip(boid.positions, boid.positions[1:]): context.move_to(position1.x, position1.y) context.line_to(position2.x, position2.y) context.stroke() # line between boid and it's target """ c_graphics.set_color("#999") context.move_to(boid.location.x, boid.location.y) context.line_to(boid.target_waypoint.location.x, boid.target_waypoint.location.y) context.stroke() """ self.redraw()
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) g.set_color("#999") for node, node2 in self.convex_hull(): g.move_to(node.x + node.pivot_x, node.y + node.pivot_y) g.line_to(node2.x + node2.pivot_x, node2.y + node2.pivot_y) node.rotation += 0.01 g.stroke() self.redraw()
def on_enter_frame(self, renderer, cell, context): g = graphics.Graphics(context) cell.rect_x = cell.rect_x or 1 cell.color = cell.color or "#f00" #g.rectangle(cell.rect_x, 0.5, 10, 10) #g.fill_stroke(cell.color, "#000") if cell.color =="#f00" and cell.data=="New": cell.color = "#0f0" renderer.animate(cell, self.rect, stroke="#0f0", x=100, duration=1)
def on_enter_frame(self, scene, context): self.roller.roll() self.roller2.roll() g = graphics.Graphics(context) g.move_to(*self.poly[0]) for dot in self.poly[1:]: g.line_to(*dot) g.stroke("#f0f") self.redraw( ) # this is how to get a constant redraw loop (say, for animation)
def on_enter_frame(self, scene, context): c_graphics = graphics.Graphics(context) c_graphics.set_line_style(width=0.5) c_graphics.set_color("#AA00FF") if len(self.flock) < 40: self.flock.append(Boid(Vector2(100, 100), 2.0, 0.05)) for boid in self.flock: boid.run(self.flock, context) context.stroke() context.fill() self.redraw()
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) # on expose is called when we are ready to draw for i, segment in reversed(list(enumerate(self.segments))): if segment.width: g.rectangle(segment.x - segment.width / 2.0, segment.y - segment.width / 2.0, segment.width, segment.width, 3) g.fill(segment.color, 0.5) else: del self.segments[i] self.redraw()
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) g.fill_area(0, 0, self.width, self.height, self.colors[0]) self.container.x = self.width / 2 self.container.y = self.height / 2 #print self.start_date.strftime("%d %b, %Y") self.start_date_label.text = self.start_date.strftime("%d %b, %Y") self.start_date_label.x = self.width / 2 - self.start_date_label.width self.start_date_label.y = self.height / 2 g.move_to(self.width / 2, self.height / 2) g.line_to(self.mouse_x, self.mouse_y) g.set_line_style(width=0.5) g.stroke("#fff")
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) g.fill_area(0, 0, self.width, self.height, "#fff", 0.08) if not self.particles: for i in range(self.particle_count): color = (random() * 0.8, random() * 0.8, random() * 0.8) self.particles.append(Particle(random() * self.width, random() * self.height, color)) g.set_line_style(width=0.3) for particle in self.particles: particle.update(self.mouse_x, self.mouse_y) g.move_to(particle.prev_x, particle.prev_y) g.line_to(particle.x, particle.y) g.stroke(particle.color) self.redraw()
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) for particle in reversed(self.tail): if particle.follow: new_x, new_y = particle.follow.x, particle.follow.y g.move_to(particle.x, particle.y) g.line_to(particle.follow.x, particle.follow.y) g.stroke(particle.color) else: new_x, new_y = self.mouse_x, self.mouse_y if abs(particle.x - new_x) + abs(particle.y - new_y) > 0.01: self.animate(particle, x = new_x, y = new_y, duration = 0.3, easing = Easing.Cubic.ease_out) if abs(self.tail[0].x - self.tail[-1].x) + abs(self.tail[0].y - self.tail[-1].y) > 1: self.redraw() # redraw if the tail is not on the head
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) for i, coords in enumerate(reversed(self.coords)): x, y = coords if i == len(self.coords) - 1: alpha = 1 else: alpha = float(i + 1) / len(self.coords) / 2 g.rectangle(x - self.radius, y - self.radius, self.radius * 2, self.radius * 2, 3) g.fill("#999", alpha) if len(self.coords) > 1: self.coords.pop(-1) self.redraw() # constant redraw (maintaining the requested frame rate)
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) g.save_context() g.rectangle(0, 0, self.mouse_x, self.height) g.clip() g.move_to(20, 100) g.show_label("Hello", font_desc="Sans Serif 150", color="#fff") g.restore_context() g.save_context() g.rectangle(self.mouse_x, 0, self.width, self.height) g.clip() g.move_to(20, 100) g.show_label("Hello", font_desc="Sans Serif 150", color="#000") g.restore_context() self.redraw()
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) g.set_line_style(width=0.5) self.triangulate() g.set_color("#666") for edge in self.edges: context.move_to(edge.point1.x, edge.point1.y) context.line_to(edge.point2.x, edge.point2.y) context.save() context.translate((edge.point1.x + edge.point2.x) / 2, (edge.point1.y + edge.point2.y) / 2) context.save() context.rotate((edge.point2 - edge.point1).heading()) context.move_to(-5, 0) g.show_label(str(edge.left_face)) context.restore() context.save() context.rotate((edge.point1 - edge.point2).heading()) context.move_to(-5, 0) g.show_label(str(edge.right_face)) context.restore() context.restore() context.stroke() if self.draw_circles: for centre in self.centres: g.set_color("#f00", 0.1) context.arc(centre.x, centre.y, centre.radius, 0, 2.0 * math.pi) context.fill_preserve() context.stroke() g.set_color("#a00") context.rectangle(centre.x - 1, centre.y - 1, 2, 2) context.stroke()
def on_enter_frame(self, scene, context): # you could do all your drawing here, or you could add some sprites g = graphics.Graphics(context) #g.move_to(10, 200) #g.line_to(self.width - 10, 200) #g.stroke("#eee") self.roller_container.rotation += 0.01 self.roller_container2.rotation += 0.01 self.roller_container.x = self.roller_container2.x = self.width / 2 self.roller_container.y = self.roller_container2.y = self.height / 2 self.roller.size = self.width self.roller.roll() self.roller2.size = self.width self.roller2.roll() self.redraw( ) # this is how to get a constant redraw loop (say, for animation)
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) if self.prev_width != self.width or self.prev_height != self.height: self.x, self.y = 0, 0 if self.x == 0 and self.y == 0: g.fill_area(0, 0, self.width, self.height, "#fff") for i in range(1000): self.x = math.sin(self.a * self.y) - math.cos(self.b * self.x) self.y = math.sin(self.c * self.x) - math.cos(self.d * self.y) x = int(self.x * self.width * 0.2 + self.width / 2) y = int(self.y * self.height * 0.2 + self.height / 2) g.rectangle(x, y, 1, 1) g.fill("#000", 0.08) self.prev_width, self.prev_height = self.width, self.height self.redraw()
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) g.fill_area(0, 0, self.width, self.height, "#fff", 0.08) if not self.particles: for i in range(self.particle_count): self.particles.append( Particle(random() * self.width, random() * self.height)) self.target.update_position(self.width, self.height) g.set_line_style(width=0.3) for particle in self.particles: particle.update(self.target.location.x, self.target.location.y) g.move_to(particle.prev_x, particle.prev_y) g.line_to(particle.x, particle.y) g.set_color("#000") g.stroke() self.redraw()
def on_enter_frame(self, scene, context): c_graphics = graphics.Graphics(context) c_graphics.set_line_style(width=0.5) if not self.centres: #reset when adding nodes self.centres, self.segments = self.delauney() c_graphics.set_color("#666") for node, node2 in self.segments: context.move_to(node.x, node.y) context.line_to(node2.x, node2.y) context.stroke() if self.draw_circles: for node, radius in self.centres: c_graphics.set_color("#f00", 0.3) context.arc(node[0], node[1], radius, 0, 2.0 * math.pi) context.fill_preserve() context.stroke() c_graphics.set_color("#a00") context.rectangle(node[0] - 1, node[1] - 1, 2, 2) context.stroke()
def on_enter_frame(self, scene, context): g = graphics.Graphics(context) data = [{ 'claimed': 0.7466666666666667, 'speed': 'fast', 'time': datetime.timedelta(0, 2, 52537) }, { 'claimed': 1.7813333333333334, 'speed': 'fast', 'time': datetime.timedelta(0, 4, 449440) }, { 'claimed': 2.965333333333333, 'speed': 'fast', 'time': datetime.timedelta(0, 9, 171014) }, { 'claimed': 45.424, 'speed': 'slow', 'time': datetime.timedelta(0, 16, 733329) }, { 'claimed': 0.8, 'speed': 'fast', 'time': datetime.timedelta(0, 19, 697931) }, { 'claimed': 1.44, 'speed': 'fast', 'time': datetime.timedelta(0, 21, 693617) }, { 'claimed': 0.4053333333333333, 'speed': 'fast', 'time': datetime.timedelta(0, 23, 404403) }, { 'claimed': 0.6453333333333333, 'speed': 'fast', 'time': datetime.timedelta(0, 25, 592150) }, { 'claimed': 13.530666666666667, 'speed': 'slow', 'time': datetime.timedelta(0, 28, 753307) }, { 'claimed': 0.7253333333333334, 'speed': 'fast', 'time': datetime.timedelta(0, 30, 684011) }, { 'claimed': 0.192, 'speed': 'fast', 'time': datetime.timedelta(0, 32, 345738) }, { 'claimed': 1.2906666666666666, 'speed': 'fast', 'time': datetime.timedelta(0, 34, 676201) }, { 'claimed': 24.528, 'speed': 'slow', 'time': datetime.timedelta(0, 39, 406208) }] #data = [{'claimed': 0.21333333333333335, 'speed': 'fast', 'time': datetime.timedelta(0, 0, 873499)}, {'claimed': 0.4, 'speed': 'fast', 'time': datetime.timedelta(0, 2, 281331)}, {'claimed': 1.3226666666666667, 'speed': 'fast', 'time': datetime.timedelta(0, 4, 116499)}, {'claimed': 3.008, 'speed': 'fast', 'time': datetime.timedelta(0, 6, 930709)}, {'claimed': 0.896, 'speed': 'fast', 'time': datetime.timedelta(0, 8, 921856)}, {'claimed': 0.608, 'speed': 'fast', 'time': datetime.timedelta(0, 9, 936559)}, {'claimed': 51.19466666666667, 'speed': 'fast', 'time': datetime.timedelta(0, 17, 55576)}, {'claimed': 15.829333333333333, 'speed': 'fast', 'time': datetime.timedelta(0, 20, 666332)}, {'claimed': 3.8026666666666666, 'speed': 'fast', 'time': datetime.timedelta(0, 24, 745785)}] g.set_line_style(3) g.translate(0.5, 0.5) g.translate(10, 50) g.save_context() self.time_volume(g, data) g.restore_context() g.translate(0, 150) g.save_context() self.just_volume(g, data) g.restore_context() g.translate(0, 100) g.save_context() self.duration_vs_volume(g, data) g.restore_context()
def on_enter_frame(self, scene, context): # you could do all your drawing here, or you could add some sprites g = graphics.Graphics(context)
def on_enter_frame(self, scene, context): if not self.data and not self.show_empty: return g = graphics.Graphics(context) g.set_line_style(width=1) days = (self.end_date - self.start_date).days full_days = [] self.clrscr() alto_bar = 15 alto_label = 8 anchos_areas = [] anchos_totales = [] for area in self.lineas: # Totales. Necesito duplicar el bucle porque el ancho de los # totales me hará falta después para las líneas horizontales. ancho_label_total = self.create_label_total(area, alto_label, alto_bar) anchos_totales.append(ancho_label_total) offset_totales = max(anchos_totales) + 5 # Por la derecha. for area in self.lineas: ancho_label_nombre = self.create_label_area(area, alto_label, alto_bar, offset_totales) anchos_areas.append(ancho_label_nombre) if self.lineas: # Una línea más debajo del último area hline = graphics.Rectangle(self.width + offset_totales, 1, fill = "#000") hline.x = 0 hline.y = 27 + len(self.lineas) * alto_bar self.grid.append(hline) self.add_child(hline) offset_labels = max(anchos_areas) + 5 # píxeles. Pero reales, no # los píxeles de la escena (day_pixel & co.), que no van 1:1. lanno = graphics.Label(`self.start_date.year`, 16, "#999") offset_labels = max(offset_labels, lanno.measure(`self.start_date.year`)[0]) lanno.y = 0 lanno.x = offset_labels - lanno.measure(`self.start_date.year`)[0] self.add_child(lanno) # Cuento el número de barras que tendré que dibujar cada día: for day in range(days): current_date = self.start_date + dt.timedelta(days = day) #if not self.day_counts[current_date]: # continue # "Comprime" la gráfica ignorando días vacíos. full_days.append(self.day_counts[current_date]) # Y ahora calculo las dimensiones en función de los días a representar. # El +1 es porque al haber tantos datos representados, la división # sale a veces (las más) un poco al alza, provocando que las barras # vayan más allá del borde del canvas. self.day_pixel = (float(self.width - offset_labels - offset_totales) / (len(full_days) + 1)) self.hour_pixel = self.day_pixel / 24 cur_x = offset_labels pixel_width = max(round(self.day_pixel), 1) dia = self.start_date for lista_tareas_day in full_days: #cur_x += round(self.hour_pixel) #print "lista_tareas_day", lista_tareas_day self.create_vlines(full_days, cur_x, dia) # "Pinto" de la más larga a la más corta para distinguir cómo se # superponen. zbuffer = calculate_zbuffer(lista_tareas_day) for numtarea_del_dia, tarea in enumerate(lista_tareas_day): self.create_bar(tarea, alto_bar, cur_x, zbuffer[tarea]) self.create_portion_carga_linea(cur_x, numtarea_del_dia, pixel_width) dia += dt.timedelta(days = 1) cur_x += round(pixel_width) # Una vez que está todo "pintado", es hora de ajustar bien el ancho y # calcular la x donde se renderizarán los totales: for area in self.labels['totales']: ltotal = self.labels['totales'][area] ltotal.x = self.width - offset_totales if DEBUG: print "-" * 80
def _draw(self, context, opacity=1, *args, **kwargs): self.get_visible_range() col_widths = self._get_col_widths() width = self.width row_height = self.get_row_height() g = graphics.Graphics(context) x, y = 0, 0 Widget._draw(self, context, opacity, *args, **kwargs) editor = None # suboptimal workaround for case when the list is packed in a vbox and then in a scrollbox # TODO - generalize and sniff out the actual crop area if self.parent and self.parent.parent and self.parent.parent and isinstance( self.parent.parent.parent, ScrollArea): scrollbox = self.parent.parent list_y = self.y + self.parent.y else: list_y = self.y scrollbox = self.parent g.rectangle(0, 0, scrollbox.width, scrollbox.height) g.clip() for row_idx in range(*self.get_visible_range()): y = row_idx * row_height row = self.rows[row_idx] state = "normal" if row == self.current_row: state = "current" elif row == self._hover_row: state = "highlight" context.save() context.translate(x, y + self.y) context.rectangle(0, 0, width, row_height) self.paint_row_background(g, row_idx, state, self.enabled) context.clip() col_x = 0 for i, (data, renderer, col_width) in enumerate( zip(row, self.renderers, col_widths)): renderer.set_data(data) renderer.render(context, col_width, row_height, row[i], state, self.enabled) renderer.restore_data() context.translate(col_width, 0) # TODO - put some place else if renderer._cell and hasattr( renderer, "_editor") and renderer._cell['col'] == i: renderer._editor.x, renderer._editor.alloc_w = col_x, col_width - 1 editor = renderer._editor col_x += col_width context.restore() if editor: # repaint editor as it is stepped all over context.save() context.translate(0, self.y) editor._draw(context, parent_matrix=self.get_matrix()) context.restore()