Skip to content

Commit

Permalink
Push pixel_coords_to_space_coords to Window
Browse files Browse the repository at this point in the history
  • Loading branch information
3b1b committed Aug 24, 2021
1 parent 8645894 commit c635f19
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
11 changes: 0 additions & 11 deletions manimlib/camera/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,6 @@ def resize_frame_shape(self, fixed_dimension=0):
self.frame.set_height(frame_height)
self.frame.set_width(frame_width)

def pixel_coords_to_space_coords(self, px, py, relative=False):
pw, ph = self.fbo.size
fw, fh = self.get_frame_shape()
fc = self.get_frame_center()
if relative:
return 2 * np.array([px / pw, py / ph, 0])
else:
# Only scale wrt one axis
scale = fh / ph
return fc + scale * np.array([(px - pw / 2), (py - ph / 2), 0])

# Rendering
def capture(self, *mobjects, **kwargs):
self.refresh_perspective_uniforms()
Expand Down
13 changes: 12 additions & 1 deletion manimlib/window.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import moderngl_window as mglw
from moderngl_window.context.pyglet.window import Window as PygletWindow
from moderngl_window.timers.clock import Timer
Expand Down Expand Up @@ -59,7 +60,17 @@ def find_initial_position(self, size):

# Delegate event handling to scene
def pixel_coords_to_space_coords(self, px, py, relative=False):
return self.scene.camera.pixel_coords_to_space_coords(px, py, relative)
pw, ph = self.size
fw, fh = self.scene.camera.get_frame_shape()
fc = self.scene.camera.get_frame_center()
if relative:
return np.array([px / pw, py / ph, 0])
else:
return np.array([
fc[0] + px * fw / pw - fw / 2,
fc[1] + py * fh / ph - fh / 2,
0
])

def on_mouse_motion(self, x, y, dx, dy):
super().on_mouse_motion(x, y, dx, dy)
Expand Down

0 comments on commit c635f19

Please sign in to comment.