Sha256: 77fe4ccded6e7399349eb0138d3845397f9809fe0c8880baa8c7ab92388b1dbe

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require_relative "shape_helper"

class Scarpe
  class WebviewStar < Scarpe::WebviewWidget
    include ShapeHelper
    def initialize(properties)
      super(properties)
    end

    def element(&block)
      HTML.render do |h|
        h.div(id: html_id, style: style) do
          h.svg(width: @outer, height: @outer, style: "fill:#{@color};") do
            h.polygon(points: star_points, style: "stroke:#{stroke_color};stroke-width:2")
          end
          block.call(h) if block_given?
        end
      end
    end

    private

    def style
      {
        width: Dimensions.length(@width),
        height: Dimensions.length(@height),
      }
    end

    def star_points
      get_star_points.join(",")
    end

    def get_star_points
      angle = 2 * Math::PI / @points
      coordinates = []

      @points.times do |i|
        outer_angle = i * angle
        inner_angle = outer_angle + angle / 2

        coordinates.concat(get_coordinates(outer_angle, inner_angle))
      end

      coordinates
    end

    def get_coordinates(outer_angle, inner_angle)
      outer_x = @outer / 2 + Math.cos(outer_angle) * @outer / 2
      outer_y = @outer / 2 + Math.sin(outer_angle) * @outer / 2

      inner_x = @outer / 2 + Math.cos(inner_angle) * @inner / 2
      inner_y = @outer / 2 + Math.sin(inner_angle) * @inner / 2

      [outer_x, outer_y, inner_x, inner_y]
    end

    def stroke_color
      "black"
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
lacci-0.2.1 lib/scarpe/wv/star.rb
scarpe-0.2.1 lib/scarpe/wv/star.rb
scarpe-0.2.0 lib/scarpe/wv/star.rb