Hier ist der Code:
Code: Select all
class InteractivePyglet(Scene):
def construct(self):
# 1. Create a Manim object
self.circ = Circle(fill_opacity=0.5, color=BLUE)
self.sq = Square(color=RED).shift(RIGHT * 3)
self.add(self.circ, self.sq)
self.interactive_embed()
def on_key_press(self,symbol, modifiers):
from pyglet.window import key as pyglet_key
""" When pressing key S, the circle will then move to the mouse cursor"""
if symbol==pyglet_key.S:
mouse_pos = self.mouse_point.get_center()
# Make the circle follow the mouse
self.circ.move_to(mouse_pos)
# Logic: Change square color if circle is near
if np.linalg.norm(self.circ.get_center() - self.sq.get_center()) < 2:
self.sq.set_color(YELLOW)
else:
self.sq.set_color(RED)
# I used "manim -qm -p --renderer=opengl file.py InteractivePyglet to render this scene to make interaction.
Code: Select all
class InteractivePyglet(Scene):
def construct(self):
# 1. Create a Manim object
self.circ = Circle(fill_opacity=0.5, color=BLUE)
self.sq = Square(color=RED).shift(RIGHT * 3)
self.add(self.circ, self.sq)
self.interactive_embed()
def on_mouse_motion(self,symbol, modifiers):
#trying to use om_mouse_motion to replace om_key_press to achieve goal
mouse_pos = self.mouse_point.get_center()
# Make the circle follow the mouse
self.circ.move_to(mouse_pos)
# Logic: Change square color if circle is near
if np.linalg.norm(self.circ.get_center() - self.sq.get_center()) < 2:
self.sq.set_color(YELLOW)
else:
self.sq.set_color(RED)
Gibt es Möglichkeiten für eine sofortige Interaktion?
Mobile version