Class: TileKit::Icon

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

Overview

this class represents an icon that can be drawn on an Image

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, size, peak_position, clickable_area) ⇒ Icon

initializes the icon with a path, image size [width, height], the peak# position (where the pointer should be placed) [x, y] and a bounding box that represents a clickable area

path

the path of the image (test.png)

size

the size of the immage as array [width, height]

peak_position

the peak position as array [x, y]

clickable_area

the clickable area of the icon as array [top, left, bottom, right]



17
18
19
20
21
22
# File 'lib/tilekit.rb', line 17

def initialize(path, size, peak_position, clickable_area)
  @image = Magick::Image.read(path)[0]
  @size_x, @size_y = size
  @peak_x, @peak_y = peak_position
  @shift_x, @shift_y, @width, @height = clickable_area
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



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

def image
  @image
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

Instance Method Details

#bounding_box(lat, lng, zoom) ⇒ Object

returns a boundingbox (with lat/lng) that contains the bounds of the image for the passed position



35
36
37
38
39
# File 'lib/tilekit.rb', line 35

def bounding_box(lat, lng, zoom)
  top, left = MapKit.shift_latlng(lat, lng, @shift_x - @peak_x, @shift_y - @peak_y, zoom)
  bottom, right = MapKit.shift_latlng(top, left, @width, @height, zoom)
  MapKit::BoundingBox.new(top, left, bottom, right, zoom)
end

#draw(canvas, x, y) ⇒ Object

draws the icon on the canvas at passed position (x, y)



25
26
27
28
29
30
31
# File 'lib/tilekit.rb', line 25

def draw(canvas, x, y)
  # position icon at peak point
  x, y = x - @peak_x, y - @peak_y

  # copy image
  canvas.composite!(@image, Magick::ForgetGravity, x, y, Magick::CopyCompositeOp)
end