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

Feature: Automatic rerendering on resize #272

Closed
Closed
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5bdd612
Improved protection for non-numerical input.
kplindegaard Feb 27, 2017
6d99c3a
Fixed #124: Improved protection for non-numerical input.
kplindegaard Apr 8, 2017
26ee1c3
Doc updates for 1.3.3
kplindegaard Apr 9, 2017
4ff0516
Merge upstream changes
kplindegaard May 4, 2017
bca4bf6
Completed #17 for Donut.
kplindegaard May 4, 2017
acf5c7b
Improved pointer tip icon feature and added documentation for it.
kplindegaard May 4, 2017
d620fdb
Doc updates, new version number.
kplindegaard May 12, 2017
0fd154d
Sorry, I missed a couple of files. See previous commit message.
kplindegaard May 12, 2017
a7b7430
Merged and minified issue #139.
kplindegaard Jul 8, 2017
0b39bf1
Updated docs and package files.
kplindegaard Jul 8, 2017
3f28ff8
Merge remote-tracking branch 'upstream/gh-pages' into gh-pages
kplindegaard Jul 24, 2017
b0cb92d
Issue #143: Prevent requestAnimationFrame() callbacks from piling up.
kplindegaard Jul 24, 2017
beae201
Correct use of options.generateGradient as discussed in PR #65.
kplindegaard Aug 17, 2017
06f2411
Merge branch 'gh-pages' into gh-pages
kplindegaard Aug 17, 2017
1874e13
Merged upstream changes.
kplindegaard Mar 31, 2018
171c743
Fixed merge conflict
kplindegaard Mar 31, 2018
8ddf9aa
Fixed cleanup of AnimationUpdater
kplindegaard Jun 15, 2019
b172f8e
Merge remote-tracking branch 'upstream/gh-pages' into gh-pages
kplindegaard Jun 15, 2019
c625910
Merge branch 'cleanup-animationupdater' into gh-pages
kplindegaard Jun 15, 2019
236f664
AnimationUpdate removes finished references to prevent memory leaks.
kplindegaard Jun 15, 2019
58bf832
Fix merge conflicts
kplindegaard Feb 17, 2024
690766b
Merge remote-tracking branch 'upstream/gh-pages' into gh-pages
kplindegaard Feb 18, 2024
9ff20c6
Merge remote-tracking branch 'upstream/gh-pages' into gh-pages
kplindegaard Mar 16, 2024
feefa8e
Merge branch 'bernii:gh-pages' into gh-pages
kplindegaard Jul 7, 2024
a188b67
Merge branch 'gh-pages' of github.com:kplindegaard/gauge.js into gh-p…
kplindegaard Jul 7, 2024
a500486
Resize gauge and donut on resize events
kplindegaard Jul 7, 2024
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
46 changes: 40 additions & 6 deletions dist/gauge.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ class BaseGauge extends ValueUpdater
value = parseFloat(value) || Number(value)
return if isFinite(value) then value else 0

attachResizeHandler: () ->
if typeof ResizeObserver != 'undefined'
@resizeObserver = new ResizeObserver (entries) =>
@handleResize(entries)
@resizeObserver.observe(@canvas)

handleResize: (entries) ->
for entry in entries
if entry.target.id == @canvas.id
@canvas.height = entry.contentRect.height
@canvas.width = entry.contentRect.width
@resize()

resize: () ->
# Must be overridden

detachAll: () ->
if @resizeObserver?
@resizeObserver.unobserve(@canvas)

class TextRenderer
constructor: (@el, @fractionDigits) ->

Expand Down Expand Up @@ -316,7 +336,11 @@ class Gauge extends BaseGauge

@gp = [new GaugePointer(@)]
@setOptions()

@attachResizeHandler()

resize: (s) ->
@setOptions(@options)
@set(@lastValue)

setOptions: (options = null) ->
super(options)
Expand All @@ -332,7 +356,7 @@ class Gauge extends BaseGauge

for gauge in @gp
gauge.setOptions(@options.pointer)
gauge.render()
# gauge.render()
@render()
return @

Expand All @@ -347,6 +371,7 @@ class Gauge extends BaseGauge
@percentColors[i] = { pct: @options.percentColors[i][0], color: { r: rval, g: gval, b: bval } }

set: (value) ->
@lastValue = value
if not (value instanceof Array)
value = [value]
# Ensure values are OK
Expand Down Expand Up @@ -547,7 +572,7 @@ class Gauge extends BaseGauge
if @options.gradientType == 0
start = radius - @lineWidth / 2
stop = radius + @lineWidth / 2
fillStyle = this.ctx.createRadialGradient(w, h, start, w, h, stop)
fillStyle = this.ctx.createRadialGradient(w, h, Math.max(start, 1), w, h, stop)
else
fillStyle = this.ctx.createLinearGradient(0, 0, w, 0)
fillStyle.addColorStop(0, @options.colorStart)
Expand Down Expand Up @@ -603,6 +628,15 @@ class BaseDonut extends BaseGauge
@canvas = window.G_vmlCanvasManager.initElement(@canvas)
@ctx = @canvas.getContext('2d')
@setOptions()
@attachResizeHandler()
@render()

resize: () ->
@lineWidth = @canvas.height * @options.lineWidth
@radius = @options.radiusScale * (Math.min(@canvas.height, @canvas.width) / 2 - @lineWidth / 2)
start = @radius - @lineWidth / 2
stop = @radius + @lineWidth / 2
@options.strokeColor = @strokeGradient(@canvas.width / 2, @canvas.height / 2, start, stop)
@render()

getAngle: (value) ->
Expand All @@ -611,7 +645,7 @@ class BaseDonut extends BaseGauge
setOptions: (options = null) ->
super(options)
@lineWidth = @canvas.height * @options.lineWidth
@radius = @options.radiusScale * (@canvas.height / 2 - @lineWidth / 2)
@radius = @options.radiusScale * (Math.min(@canvas.height, @canvas.width) / 2 - @lineWidth / 2)
return @

set: (value) ->
Expand Down Expand Up @@ -642,7 +676,7 @@ class BaseDonut extends BaseGauge
start = @radius - @lineWidth / 2
stop = @radius + @lineWidth / 2

grdFill = @ctx.createRadialGradient(w, h, start, w, h, stop)
grdFill = @ctx.createRadialGradient(w, h, Math.max(start, 1), w, h, stop)
grdFill.addColorStop(0, @options.colorStart)
grdFill.addColorStop(1, @options.colorStop)

Expand All @@ -661,7 +695,7 @@ class BaseDonut extends BaseGauge

class Donut extends BaseDonut
strokeGradient: (w, h, start, stop) ->
grd = @ctx.createRadialGradient(w, h, start, w, h, stop)
grd = @ctx.createRadialGradient(w, h, Math.max(start, 1), w, h, stop)
grd.addColorStop(0, @options.shadowColor)
grd.addColorStop(0.12, @options._orgStrokeColor)
grd.addColorStop(0.88, @options._orgStrokeColor)
Expand Down