Class: Dommy::CanvasRenderingContext2D

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods
Defined in:
lib/dommy/html_canvas_element.rb

Overview

The 2D drawing context — every drawing call is a no-op, state setters are remembered (so ctx.fillStyle round-trips), and read-backs return zeroed data of the right shape. Enough for asset loaders / fingerprint probes / chart feature-detection to run without throwing.

Constant Summary collapse

STATE_DEFAULTS =

Drawing-state attributes that round-trip a value (default per spec where it matters; a plain "" otherwise).

{
  "fillStyle" => "#000000", "strokeStyle" => "#000000",
  "globalAlpha" => 1.0, "globalCompositeOperation" => "source-over",
  "lineWidth" => 1.0, "lineCap" => "butt", "lineJoin" => "miter", "miterLimit" => 10.0,
  "lineDashOffset" => 0.0, "font" => "10px sans-serif", "textAlign" => "start",
  "textBaseline" => "alphabetic", "direction" => "inherit",
  "shadowBlur" => 0.0, "shadowColor" => "rgba(0, 0, 0, 0)",
  "shadowOffsetX" => 0.0, "shadowOffsetY" => 0.0,
  "imageSmoothingEnabled" => true, "imageSmoothingQuality" => "low", "filter" => "none"
}.freeze

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Constructor Details

#initialize(canvas) ⇒ CanvasRenderingContext2D

Returns a new instance of CanvasRenderingContext2D.



105
106
107
108
# File 'lib/dommy/html_canvas_element.rb', line 105

def initialize(canvas)
  @canvas = canvas
  @state = STATE_DEFAULTS.dup
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dommy/html_canvas_element.rb', line 133

def __js_call__(method, args)
  case method
  # All drawing/state-machine operations: nothing is painted.
  when "save", "restore", "scale", "rotate", "translate", "transform", "setTransform",
       "resetTransform", "getTransform", "beginPath", "closePath", "moveTo", "lineTo",
       "bezierCurveTo", "quadraticCurveTo", "arc", "arcTo", "ellipse", "rect", "roundRect",
       "fill", "stroke", "clip", "fillRect", "strokeRect", "clearRect", "fillText", "strokeText",
       "drawImage", "putImageData", "setLineDash", "drawFocusIfNeeded", "scrollPathIntoView", "reset"
    nil
  when "getLineDash" then []
  when "measureText" then TextMetrics.new(args[0].to_s)
  when "createLinearGradient", "createRadialGradient", "createConicGradient", "createPattern"
    CanvasGradient.new
  when "getImageData" then ImageData.new(args[2].to_i.abs, args[3].to_i.abs)
  when "createImageData" then created_image_data(args)
  when "isPointInPath", "isPointInStroke" then false
  when "getContextAttributes" then {}
  end
end

#__js_get__(key) ⇒ Object



110
111
112
113
114
115
# File 'lib/dommy/html_canvas_element.rb', line 110

def __js_get__(key)
  return @canvas if key == "canvas"
  return @state[key] if @state.key?(key)

  Bridge::ABSENT
end

#__js_set__(key, value) ⇒ Object



117
118
119
120
121
122
# File 'lib/dommy/html_canvas_element.rb', line 117

def __js_set__(key, value)
  return Bridge::UNHANDLED unless @state.key?(key)

  @state[key] = value
  value
end