3

I have one canvas element generated by javascript, here is the code: http://jsfiddle.net/mtvzgnmm/

<div id="circle"></div>
<script>
    $('#circle').circleProgress({
        value: 0.75,
        size: 400,
        fill: {
            gradient: ["#e57569"]
        }
    });
</script>

But I have this #circle div in another div with 500px width, and I want to create mobile version too, but I can't make my canvas 100% wide...

2 Answers 2

3

Try this in your initialization:

size: $('#circle').parent().width(),

That will get the #circle element, get the parent of that element, then set the canvas' width to the width of its parent.

This will result in a canvas that is 100% of the width of its parent.

JSFiddle: http://jsfiddle.net/mtvzgnmm/1/

1
  • @LearnCss If you end up using this solution, don't forget to mark the answer as "accepted" by using the gray checkmark. Thanks! Commented Jul 31, 2015 at 18:59
0

you can just add the style to the canvas element. For example, change this one line

var canvas = this.canvas = this.canvas || $('<canvas style="width:100%">').prependTo(this.el)[0];

So obviously better would be to add class="mobilefull"

.mobilefull{width:100%}

Then you can add that class in javascript, if that's how you're detecting mobile, or better, use media queries:

<style>
.mobilefull{}
@media only screen and (max-width: 640px){
  .mobilefull{width:100%}
}
</style>
2
  • Setting the canvas width in CSS will cause the canvas to be stretched, which it doesn't seem like OP is looking for. See this answer for details. Commented Jul 31, 2015 at 18:54
  • @MaximillianLaumeister yes, but that's generally not a bad thing. It's perfectly reasonable, as with an image, to stretch it to the width of whatever, and in the actual canvas code, you control the pixels, just like with an image you can have a hi res or low res image, besides the stretching. Commented Jul 31, 2015 at 19:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.