Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add result parameters to gradient, pattern creation methods. #46

Merged
merged 5 commits into from
Dec 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions dom.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
// might live in a separate package. This might require special care
// to avoid circular dependencies.
//
// The documentation for some of the identifiers is based on the
// MDN Web Docs by Mozilla Contributors (https://developer.mozilla.org/en-US/docs/Web/API),
// licensed under CC-BY-SA 2.5 (https://creativecommons.org/licenses/by-sa/2.5/).
//
//
// Getting started
//
Expand Down Expand Up @@ -1964,6 +1968,33 @@ func (imd *ImageData) Set(x, y int, c color.RGBA) {
imd.Data.SetIndex(index+3, c.A)
}

// CanvasGradient represents an opaque object describing a gradient.
// It is returned by the methods CanvasRenderingContext2D.CreateLinearGradient
// or CanvasRenderingContext2D.CreateRadialGradient.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient.
type CanvasGradient struct {
*js.Object
}

// AddColorStop adds a new stop, defined by an offset and a color, to the gradient.
// It panics with *js.Error if the offset is not between 0 and 1, or if the color
// can't be parsed as a CSS <color>.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient/addColorStop.
func (cg *CanvasGradient) AddColorStop(offset float64, color string) {
cg.Call("addColorStop", offset, color)
}

// CanvasPattern represents an opaque object describing a pattern.
// It is based on an image, a canvas or a video, created by the
// CanvasRenderingContext2D.CreatePattern method.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasPattern.
type CanvasPattern struct {
*js.Object
}

type TextMetrics struct {
*js.Object

Expand Down Expand Up @@ -2052,16 +2083,28 @@ func (ctx *CanvasRenderingContext2D) SetLineDash(dashes []float64) {

// Gradients and patterns

func (ctx *CanvasRenderingContext2D) CreateLinearGradient(x0, y0, x1, y1 float64) {
ctx.Call("createLinearGradient", x0, y0, x1, y1)
// CreateLinearGradient creates a linear gradient along the line given
// by the coordinates represented by the parameters.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createLinearGradient.
func (ctx *CanvasRenderingContext2D) CreateLinearGradient(x0, y0, x1, y1 float64) *CanvasGradient {
return &CanvasGradient{Object: ctx.Call("createLinearGradient", x0, y0, x1, y1)}
}

func (ctx *CanvasRenderingContext2D) CreateRadialGradient(x0, y0, r0, x1, y1, r1 float64) {
ctx.Call("createRadialGradient", x0, y0, r0, x1, y1, r1)
// CreateRadialGradient creates a radial gradient given by the coordinates of the two circles
// represented by the parameters.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createRadialGradient.
func (ctx *CanvasRenderingContext2D) CreateRadialGradient(x0, y0, r0, x1, y1, r1 float64) *CanvasGradient {
return &CanvasGradient{Object: ctx.Call("createRadialGradient", x0, y0, r0, x1, y1, r1)}
}

func (ctx *CanvasRenderingContext2D) CreatePattern(image Element, repetition string) {
ctx.Call("createPattern", image, repetition)
// CreatePattern creates a pattern using the specified image (a CanvasImageSource).
// It repeats the source in the directions specified by the repetition argument.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createPattern.
func (ctx *CanvasRenderingContext2D) CreatePattern(image Element, repetition string) *CanvasPattern {
return &CanvasPattern{Object: ctx.Call("createPattern", image, repetition)}
}

// Paths
Expand Down