Skip to content

Commit cfd17bc

Browse files
committed
Responsive margin
Added responsive margin property and tidied up responsive wheel example.
1 parent fa1fa8c commit cfd17bc

23 files changed

Lines changed: 80 additions & 3156 deletions

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016 Douglas McKechie
3+
Copyright (c) 2012-2019 Douglas McKechie
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ Wheels can be animated using GreenSock's Animation Platform (TweenMax.js) which
99
Winwheel.js Features Include:
1010
* Easy to use, highly configurable JavaScript classes.
1111
* Draw wheels using code generated segments or graphically rich images.
12+
* Responsive features so wheels display correctly on different sized devices.
1213
* Numerous text orientation, direction, size and colour options.
1314
* Random or Pre-calculated prize stopping location.
1415
* Play sounds while the wheel is spinning including a "tick" sound.
1516
* Ability to get the segment the user clicked upon.
1617
* Fully commented source code. Plenty of tutorials and other documentation.
17-
* Winwheel.js is free to use with an open source licence.
18+
* Winwheel.js is free to use with an open source license.
1819

1920
## Example
2021
```javascript
21-
var myWheel = new Winwheel({
22+
var theWheel = new Winwheel({
2223
'numSegments' : 4,
2324
'segments' :
2425
[
@@ -45,4 +46,11 @@ Please visit http://dougtesting.net/winwheel/docs to see a complete set of tutor
4546
## Maintainer
4647
Douglas McKechie https://github.com/zarocknz
4748

48-
Keep informed about Winwheel.js by following https://twitter.com/dougtesting
49+
## Please note
50+
I am not planning to do any further work on this library as my day job keeps me very busy and after 7 years of Winwheel I would rather spend
51+
any spare time I do have for personal coding on other projects.
52+
53+
So this means if you would like a version of Winwheel.js for your current JavaScript framework of choice its up to you or others in the community
54+
to create it. If you create one perhaps open an Issue with the details so others can find and use it. Thanks.
55+
56+
You are welcome to ask questions using the Issues feature of Github, but please don't be offended if I take quite a long time to respond to them. To be honest its probably quicker to ask the Stackoverflow community for help https://stackoverflow.com/search?tab=newest&q=Winwheel

Winwheel.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
The MIT License (MIT)
66
7-
Copyright (c) 2018 Douglas McKechie
7+
Copyright (c) 2012-2019 Douglas McKechie
88
99
Permission is hereby granted, free of charge, to any person obtaining a copy
1010
of this software and associated documentation files (the "Software"), to deal
@@ -26,7 +26,7 @@
2626
*/
2727

2828
// ====================================================================================================================
29-
// The constructor for the WinWheel object, a JOSN-like array of options can be passed in.
29+
// The constructor for the WinWheel object, a JSON-like array of options can be passed in.
3030
// By default the wheel is drawn if canvas object exists on the page, but can pass false as second parameter if don't want this to happen.
3131
// ====================================================================================================================
3232
function Winwheel(options, drawWheel)
@@ -61,9 +61,6 @@ function Winwheel(options, drawWheel)
6161
'imageDirection' : 'N', // Used when drawMode is segmentImage. Default is north, can also be (E)ast, (S)outh, (W)est.
6262
'responsive' : false, // If set to true the wheel will resize when the window first loads and also onResize.
6363
'scaleFactor' : 1, // Set by the responsive function. Used in many calculations to scale the wheel.
64-
//++ @TODO may want to scale stroke line width for both segments and text somehow. GetSegmentAt still needs scaling adjustment.
65-
//++ @TODO check how scaling affects animations, esp custom ones where value to move the center to etc was specified etc, or not a problem?
66-
//++ @TODO scrollbar can be a pain at smaller sizes, need to compensate for it or use window.width and height minus this to set the canvas size.
6764
};
6865

6966
// -----------------------------------------
@@ -125,7 +122,6 @@ function Winwheel(options, drawWheel)
125122
this.ctx = null;
126123
}
127124

128-
129125
// ------------------------------------------
130126
// Add array of segments to the wheel, then populate with segments if number of segments is specified for this object.
131127
this.segments = new Array(null);
@@ -144,7 +140,6 @@ function Winwheel(options, drawWheel)
144140
// Call function to update the segment sizes setting the starting and ending angles.
145141
this.updateSegmentSizes();
146142

147-
148143
// If the text margin is null then set to same as font size as we want some by default.
149144
if (this.textMargin === null) {
150145
this.textMargin = (this.textFontSize / 1.7);
@@ -166,7 +161,7 @@ function Winwheel(options, drawWheel)
166161
}
167162

168163
// ------------------------------------------
169-
// On that note, if the drawMode is image change some defaults provided a value has not been specified.
164+
// If the drawMode is image change some defaults provided a value has not been specified.
170165
if ((this.drawMode == 'image') || (this.drawMode == 'segmentImage')) {
171166
// Remove grey fillStyle.
172167
if (typeof(options['fillStyle']) === 'undefined') {
@@ -219,6 +214,7 @@ function Winwheel(options, drawWheel)
219214
this._responsiveScaleHeight = this.canvas.dataset.responsivescaleheight;
220215
this._responsiveMinWidth = this.canvas.dataset.responsiveminwidth;
221216
this._responsiveMinHeight = this.canvas.dataset.responsiveminheight;
217+
this._responsiveMargin = this.canvas.dataset.responsivemargin;
222218

223219
// Add event listeners for onload and onresize and call a function defined at the bottom
224220
// of this script which will handle that and work out the scale factor.
@@ -229,8 +225,7 @@ function Winwheel(options, drawWheel)
229225
// Finally if drawWheel is true then call function to render the wheel, segment text, overlay etc.
230226
if (drawWheel == true) {
231227
this.draw(this.clearTheCanvas);
232-
}
233-
else if (this.drawMode == 'segmentImage') {
228+
} else if (this.drawMode == 'segmentImage') {
234229
// If segment image then loop though all the segments and load the images for them setting a callback
235230
// which will call the draw function of the wheel once all the images have been loaded.
236231
winwheelToDrawDuringAnimation = this;
@@ -665,10 +660,8 @@ Winwheel.prototype.drawSegments = function()
665660
this.ctx.strokeStyle = strokeStyle;
666661

667662

668-
// Check there is a strokeStyle or fillStyle, if either the the segment is invisible so should not
669-
// try to draw it otherwise a path is began but not ended.
663+
// Check there is a strokeStyle or fillStyle, if not the segment is invisible so should not try to draw it otherwise a path is began but not ended.
670664
if ((strokeStyle) || (fillStyle)) {
671-
// ----------------------------------
672665
// Begin a path as the segment consists of an arc and 2 lines.
673666
this.ctx.beginPath();
674667

@@ -802,8 +795,7 @@ Winwheel.prototype.drawSegmentText = function()
802795
lineOffset = 0;
803796
}
804797

805-
for(let i = 0; i < lines.length; i ++) {
806-
// ---------------------------------
798+
for (let i = 0; i < lines.length; i ++) {
807799
// If direction is reversed then do things differently than if normal (which is the default - see further down)
808800
if (direction == 'reversed') {
809801
// When drawing reversed or 'upside down' we need to do some trickery on our part.
@@ -1757,7 +1749,7 @@ Winwheel.prototype.startAnimation = function()
17571749
}
17581750

17591751
// ==================================================================================================================================================
1760-
// Use same function function which needs to be outside the class for the callback when it stops because is finished.
1752+
// Use same function which needs to be outside the class for the callback when it stops because is finished.
17611753
// ==================================================================================================================================================
17621754
Winwheel.prototype.stopAnimation = function(canCallback)
17631755
{
@@ -2298,7 +2290,18 @@ function winwheelLoadedImage()
22982290
// ====================================================================================================================
22992291
function winwheelResize()
23002292
{
2301-
let width = window.innerWidth;
2293+
// By default set the margin to 40px, this can be overridden if needed.
2294+
// This is to stop the canvas going right to the right edge of the screen and being overlayed by a scrollbar though
2295+
// if the canvas is center aligned, half the magin will be applied to each side since the margin actually reduces the width of the canvas.
2296+
let margin = 40;
2297+
2298+
// If a value has been specified for this then update the margin to it.
2299+
if (typeof(winwheelToDrawDuringAnimation._responsiveMargin) !== 'undefined') {
2300+
margin = winwheelToDrawDuringAnimation._responsiveMargin;
2301+
}
2302+
2303+
// Get the current width and also optional min and max width properties.
2304+
let width = window.innerWidth - margin;
23022305
let minWidth = winwheelToDrawDuringAnimation._responsiveMinWidth;
23032306
let minHeight = winwheelToDrawDuringAnimation._responsiveMinHeight;
23042307

0 commit comments

Comments
 (0)