Class: Three::Browser::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/three/browser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle) ⇒ Element

Returns a new instance of Element.



10
11
12
# File 'lib/three/browser.rb', line 10

def initialize(handle)
  @handle = handle
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



8
9
10
# File 'lib/three/browser.rb', line 8

def handle
  @handle
end

Class Method Details

.wrap(value) ⇒ Object



14
15
16
# File 'lib/three/browser.rb', line 14

def self.wrap(value)
  value.is_a?(self) ? value : new(value)
end

Instance Method Details

#add_event_listener(name, &block) ⇒ Object Also known as: on



30
31
32
33
# File 'lib/three/browser.rb', line 30

def add_event_listener(name, &block)
  @handle.call(:addEventListener, name.to_s, block)
  self
end

#bounding_client_rectObject



37
38
39
40
41
42
43
44
45
# File 'lib/three/browser.rb', line 37

def bounding_client_rect
  rect = @handle.call(:getBoundingClientRect)
  {
    left: rect[:left].to_f,
    top: rect[:top].to_f,
    width: rect[:width].to_f,
    height: rect[:height].to_f
  }
end

#client_heightObject



22
23
24
# File 'lib/three/browser.rb', line 22

def client_height
  [@handle[:clientHeight].to_i, 1].max
end

#client_widthObject



18
19
20
# File 'lib/three/browser.rb', line 18

def client_width
  [@handle[:clientWidth].to_i, 1].max
end

#pointer_ndc(event) ⇒ Object



47
48
49
50
51
52
# File 'lib/three/browser.rb', line 47

def pointer_ndc(event)
  rect = bounding_client_rect
  x = ((event[:clientX].to_f - rect[:left]) / rect[:width]) * 2 - 1
  y = -(((event[:clientY].to_f - rect[:top]) / rect[:height]) * 2 - 1)
  [x, y]
end

#sizeObject



26
27
28
# File 'lib/three/browser.rb', line 26

def size
  [client_width, client_height]
end