The way winwheel library is designed, is to start the segments at index 1, instead of 0.
This is creating problems when trying to fetch the wheel segments from the database.
I have a column “segments” in my table, which holds this value:
[
{
"image": "./assets/img/5.png",
"strokeStyle": "black",
"size": 60,
"text": "5% korting"
},
{
"image": "./assets/img/4.png",
"strokeStyle": "black",
"size": 60,
"text": "10% korting"
},
{
"image": "./assets/img/5.png",
"strokeStyle": "black",
"size": 60,
"text": "5% korting"
},
{
"image": "./assets/img/4.png",
"strokeStyle": "black",
"size": 60,
"text": "10% korting"
},
{
"image": "./assets/img/5.png",
"strokeStyle": "black",
"size": 60,
"text": "5% korting"
},
{
"image": "./assets/img/4.png",
"strokeStyle": "black",
"size": 60,
"text": "15% korting"
}
]
No matter what I try, I cannot get the segments right AFTER creating the wheel.
WinWheel library proceeds to return 7 segments instead, the first one being null.
See below:
let theWheel;
// Fetch wheel configuration from the server
fetch('./getWheelConfig.php')
.then(response => response.json())
.then(data => {
// Logging the data fetched ( it has the right format, and returns 6 segments )
console.log(data);
// Use the fetched data to create the wheel
theWheel = new Winwheel({
'canvasId': 'canvas',
'numSegments': data.num_segments,
'outerRadius': data.outer_radius,
'drawMode': data.draw_mode,
'rotationAngle': 0,
'textFontFamily': data.text_font_family,
'textFontSize': data.text_font_size,
'textFontWeight': 'bold',
'textOrientation': data.text_orientation,
'textAlignment': data.text_alignment,
'textDirection': 'normal',
'textMargin': parseFloat(data.text_margin),
'textFillStyle': data.text_fill_style,
'textStrokeStyle': data.text_stroke_style,
'textLineWidth': parseInt(data.text_line_width),
'fillStyle': 'silver',
'strokeStyle': 'black',
'lineWidth': 1,
'clearTheCanvas': true,
'imageOverlay': data.image_overlay,
'drawText': data.draw_text,
'pointerAngle': 0,
'wheelImage': null,
'imageDirection': data.image_direction,
'responsive': data.responsive,
'segments': data.segments,
'animation': {
'type': data.animation_type,
'duration': data.animation_duration,
'spins': data.animation_spins,
'callbackFinished': 'alertPrize',
'callbackSound': 'playTick',
'soundTrigger': data.animation_sound_trigger,
},
'pins': {
'responsive': data.pins_responsive,
'fillStyle': data.pins_fill_style,
'strokeStyle': data.pins_stroke_style,
'outerRadius': parseInt(data.pins_outer_radius),
'number': parseInt(data.pins_number),
},
});
console.log(theWheel);
})
.catch(error => console.error('Error fetching wheel configuration:', error));
The data being retrieved from the database is not corrupted and is in the correct format and I can confirm this when I “console.log(data)”.
As soon as the wheel is created and I try “console.log(theWheel)” to see which segments are being added, I see 7 segments instead with the first being null.
See the object below that is being logged after wheel creation:
{
"canvasId": "canvas",
"centerX": 217,
"centerY": 217,
"outerRadius": 210,
"innerRadius": 0,
"numSegments": 6,
"drawMode": "segmentImage",
"rotationAngle": 0,
"textFontFamily": "monospace",
"textFontSize": 18,
"textFontWeight": "bold",
"textOrientation": "curved",
"textAlignment": "outer",
"textDirection": "normal",
"textMargin": 25,
"textFillStyle": "white",
"textStrokeStyle": "black",
"textLineWidth": 3,
"fillStyle": "silver",
"strokeStyle": "black",
"lineWidth": 1,
"clearTheCanvas": true,
"imageOverlay": 1,
"drawText": 1,
"pointerAngle": 0,
"wheelImage": null,
"imageDirection": "N",
"responsive": 1,
"scaleFactor": 0.9976958525345622,
"segments": [
null,
{
"size": 60,
"text": "5% korting",
"fillStyle": null,
"strokeStyle": "black",
"lineWidth": null,
"textFontFamily": null,
"textFontSize": null,
"textFontWeight": null,
"textOrientation": null,
"textAlignment": null,
"textDirection": null,
"textMargin": null,
"textFillStyle": null,
"textStrokeStyle": null,
"textLineWidth": null,
"image": "./assets/img/5.png",
"imageDirection": null,
"imgData": {},
"startAngle": 0,
"endAngle": 60
},
{
"size": 60,
"text": "10% korting",
"fillStyle": null,
"strokeStyle": "black",
"lineWidth": null,
"textFontFamily": null,
"textFontSize": null,
"textFontWeight": null,
"textOrientation": null,
"textAlignment": null,
"textDirection": null,
"textMargin": null,
"textFillStyle": null,
"textStrokeStyle": null,
"textLineWidth": null,
"image": "./assets/img/4.png",
"imageDirection": null,
"imgData": {},
"startAngle": 60,
"endAngle": 120
},
{
"size": 60,
"text": "5% korting",
"fillStyle": null,
"strokeStyle": "black",
"lineWidth": null,
"textFontFamily": null,
"textFontSize": null,
"textFontWeight": null,
"textOrientation": null,
"textAlignment": null,
"textDirection": null,
"textMargin": null,
"textFillStyle": null,
"textStrokeStyle": null,
"textLineWidth": null,
"image": "./assets/img/5.png",
"imageDirection": null,
"imgData": {},
"startAngle": 120,
"endAngle": 180
},
{
"size": 60,
"text": "10% korting",
"fillStyle": null,
"strokeStyle": "black",
"lineWidth": null,
"textFontFamily": null,
"textFontSize": null,
"textFontWeight": null,
"textOrientation": null,
"textAlignment": null,
"textDirection": null,
"textMargin": null,
"textFillStyle": null,
"textStrokeStyle": null,
"textLineWidth": null,
"image": "./assets/img/4.png",
"imageDirection": null,
"imgData": {},
"startAngle": 180,
"endAngle": 240
},
{
"size": 60,
"text": "5% korting",
"fillStyle": null,
"strokeStyle": "black",
"lineWidth": null,
"textFontFamily": null,
"textFontSize": null,
"textFontWeight": null,
"textOrientation": null,
"textAlignment": null,
"textDirection": null,
"textMargin": null,
"textFillStyle": null,
"textStrokeStyle": null,
"textLineWidth": null,
"image": "./assets/img/5.png",
"imageDirection": null,
"imgData": {},
"startAngle": 240,
"endAngle": 300
},
{
"size": 60,
"text": "15% korting",
"fillStyle": null,
"strokeStyle": "black",
"lineWidth": null,
"textFontFamily": null,
"textFontSize": null,
"textFontWeight": null,
"textOrientation": null,
"textAlignment": null,
"textDirection": null,
"textMargin": null,
"textFillStyle": null,
"textStrokeStyle": null,
"textLineWidth": null,
"image": "./assets/img/4.png",
"imageDirection": null,
"imgData": {},
"startAngle": 300,
"endAngle": 360
}
],
"animation": {
"type": "spinToStop",
"direction": "clockwise",
"propertyName": null,
"propertyValue": null,
"duration": 15,
"yoyo": false,
"repeat": null,
"easing": null,
"stopAngle": null,
"spins": 75,
"clearTheCanvas": null,
"callbackFinished": "alertPrize",
"callbackBefore": null,
"callbackAfter": null,
"callbackSound": "playTick",
"soundTrigger": "pin"
},
"pins": {
"visible": true,
"number": 24,
"outerRadius": 4,
"fillStyle": "silver",
"strokeStyle": "black",
"lineWidth": 1,
"margin": 3,
"responsive": 1
},
"canvas": {},
"ctx": {},
"pointerGuide": {
"display": false,
"strokeStyle": "red",
"lineWidth": 3
},
"_originalCanvasWidth": 434,
"_originalCanvasHeight": 434,
"_responsiveScaleHeight": "true",
"_responsiveMinWidth": "425"
}
Is there a way to make winwheel use the default index of 0 instead of 1?!
Or is there another way to get this working? I’ve tried anything but with no success.
As soon as the wheel is created, I suddenly have 7 segments ( instead of 6 being fetched from my database) and the first segment is null.
Tried:
ForEach loop inside wheel object creation
Expected:
6 segments being added to be created in the wheel object
Resulted:
7 segments with the first being null