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

Added responsive option: canvas is sized to fit in parent DOM node #64

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ var opts = {
},
colorStart: '#6FADCF', // Colors
colorStop: '#8FC0DA', // just experiment with them
strokeColor: '#E0E0E0' // to see which ones work best for you
strokeColor: '#E0E0E0', // to see which ones work best for you
responsive: false // set to true if you want the gauge to scale with the parent element
};
var target = document.getElementById('foo'); // your canvas element
var gauge = new Gauge(target).setOptions(opts); // create sexy gauge!
Expand Down
10 changes: 8 additions & 2 deletions dist/gauge.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ class BaseGauge extends ValueUpdater
@ctx.oBackingStorePixelRatio or
@ctx.backingStorePixelRatio or 1
@displayScale = devicePixelRatio / backingStorePixelRatio

if @displayScale != prevDisplayScale
if @options.responsive or @displayScale != prevDisplayScale
width = @canvas.G__width or @canvas.width
height = @canvas.G__height or @canvas.height
@canvas.width = width * @displayScale
Expand Down Expand Up @@ -297,6 +296,10 @@ class Gauge extends BaseGauge
@render()

setOptions: (options=null) ->
console.log(options)
if options && options.responsive
@canvas.G__width = @canvas.parentNode.clientWidth
@canvas.G__height = Math.min(@canvas.parentNode.clientHeight, @canvas.G__width/2)
super(options)
@configPercentColors()
@lineWidth = @canvas.height * (1 - @paddingBottom) * @options.lineWidth # .2 - .7
Expand Down Expand Up @@ -439,6 +442,9 @@ class BaseDonut extends BaseGauge
return (1 - @options.angle) * Math.PI + ((value - @minValue) / (@maxValue - @minValue)) * ((2 + @options.angle) - (1 - @options.angle)) * Math.PI

setOptions: (options=null) ->
if options && options.responsive
@canvas.G__width = Math.min(@canvas.parentNode.clientWidth, @canvas.parentNode.clientHeight)
@canvas.G__height = @canvas.G__width
super(options)
@lineWidth = @canvas.height * @options.lineWidth
@radius = @canvas.height / 2 - @lineWidth/2
Expand Down
65 changes: 36 additions & 29 deletions dist/gauge.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ <h2 id="usage">Usage</h2>
colorStart: '<span id="opt-colorStart" class="lit">0</span>', // Colors
colorStop: '<span id="opt-colorStop" class="lit">0</span>', // just experiment with them
strokeColor: '<span id="opt-strokeColor" class="lit">0</span>', // to see which ones work best for you
generateGradient: true
generateGradient: true,
responsive: false // set to true if you want the gauge to scale with the parent element
};
var target = document.getElementById('foo'); // your canvas element
var gauge = new <span id="class-code-name" class="typ">Gauge</span>(target).setOptions(opts); // create sexy gauge!
Expand Down Expand Up @@ -367,4 +368,4 @@ <h2>Contact</h2>

</script>
</body>
</html>
</html>