Center aligning text when using an animation block (Confetti) or overlay in a question | XM Community
Skip to main content

Hi All,

I wanted to spice up an end question of a survey a little by adding a little snowstorm / confetti (and at the same time improving my coding and HTML knowledge!) however I’m struggling with scaling and alignment of text etc. as I need to make it look just as good on a mobile screen.

 

I’d be grateful for your help and guidance on what styles and aspects need to change to manage this.

 

Thanks

 

Rod Pestell

 

----------------------------------------

 

I found some code courtesy of https://community.hubspot.com/t5/CMS-Development/Trying-to-create-a-confetti-effect-in-html/m-p/13304 which provided this: https://codepen.io/linrock/pen/nMadjQ as a staring point.  I worked out how to add the canvas element to the HTML of my question and also worked out how to add the code into the javascript of the question (you need to change one part where it says window.onload to Qualtrics.surveyengine.addonload).  Once working I then needed to work out how to overlay the text centrally.  This took me down the route of using the ‘position: absolute’ style but have now got stuck as I need to centre it and the normal styles suggested online don’t seem to work as a result.

 

My challenges are given in the annotated picture below but in short I need to

  1. Make the text centred vertically and horizontally relative to the confetti
  2. The confetti width and height need to be approx a ratio of 16:9 and a little larger than the height and width of the text.
  3. The confetti is currently squished because I’m forcing the height to be 200px so I need to make sure that they don’t appear squished.
  4. Also (as per the annotated pic) you can see that the colour of the confetti has been impacted by something in my normal CSS look and feel of the survey so I’d be grateful for some guidance on how I can tell it to ignore it and use the confetti colours as depicted in the code.

 

Below is the code which I’ve put in the javascript part of the question in Qualtrics.

 

I’ve included the basic HTML of my question that I’ve used in the codepen to show you the layout however it would probably be easier just to replicate this question (as per the annotated pic) in your Qualtrics system (but code pen is easier to change things and test).

 

This is the HTML code I used in codepen.  The various styles used will need changing obviosuly to fulfill the above points I need help on:

<div style="text-align: center;"><span style="font-size:26px; position: absolute; vertical-align: middle; margin-top: 4%">${e://Field/Segment_API_Name}!!</span>
<canvas id="world" style="display: block; position: relative; pointer-events: none; background: transparent; width: 80%;margin-left: 10%; height: 200px;"></canvas></div>

 

I changed the CSS in codepen to this:

html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: transparent;
}

canvas {
display:block;
position:absolute;
}

 

This is the result in code pen.  Notice I am forcing the location of the text but it needs to be relative and the confetti needs to be just a little taller and wider than the text.

 

Below is what it looks like in my Qualtrics preview at present.  I’ve annotated the problems and what I am trying to do.

 

 

 

 

This is the full code i have in the Qualtrics question.  I had to change one element from windows.onload to Qualtrics.SurveyEngine.addOnload to get it to work.

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

(function() {
var COLORS, Confetti, NUM_CONFETTI, PI_2, canvas, confetti, context, drawCircle, i, range, resizeWindow, xpos;

NUM_CONFETTI = 350;

COLORS =
PI_2 = 2 * Math.PI;

canvas = document.getElementById("world");

context = canvas.getContext("2d");

window.w = 0;

window.h = 0;

resizeWindow = function() {
window.w = canvas.width = window.innerWidth;
return window.h = canvas.height = window.innerHeight;
};

window.addEventListener('resize', resizeWindow, false);

Qualtrics.SurveyEngine.addOnload = function() {
return setTimeout(resizeWindow, 0);
};

range = function(a, b) {
return (b - a) * Math.random() + a;
};

drawCircle = function(x, y, r, style) {
context.beginPath();
context.arc(x, y, r*2, 0, PI_2, false);
context.fillStyle = style;
return context.fill();
};

xpos = 0.5;

document.onmousemove = function(e) {
return xpos = e.pageX / w;
};

window.requestAnimationFrame = (function() {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) {
return window.setTimeout(callback, 1000 / 60);
};
})();

Confetti = class Confetti {
constructor() {
this.style = COLORS<~~range(0, 5)];
this.rgb = `rgba(${this.style~0]},${this.styler1]},${this.styleb2]}`;
this.r = ~~range(2, 6);
this.r2 = 2 * this.r;
this.replace();
}

replace() {
this.opacity = 0;
this.dop = 0.03 * range(1, 4);
this.x = range(-this.r2, w - this.r2);
this.y = range(-20, h - this.r2);
this.xmax = w - this.r;
this.ymax = h - this.r;
this.vx = range(0, 2) + 8 * xpos - 5;
return this.vy = 0.7 * this.r + range(-1, 1);
}

draw() {
var ref;
this.x += this.vx;
this.y += this.vy;
this.opacity += this.dop;
if (this.opacity > 1) {
this.opacity = 1;
this.dop *= -1;
}
if (this.opacity < 0 || this.y > this.ymax) {
this.replace();
}
if (!((0 < (ref = this.x) && ref < this.xmax))) {
this.x = (this.x + this.xmax) % this.xmax;
}
return drawCircle(~~this.x, ~~this.y, this.r, `${this.rgb},${this.opacity})`);
}

};

confetti = (function() {
var j, ref, results;
results = o];
for (i = j = 1, ref = NUM_CONFETTI; (1 <= ref ? j <= ref : j >= ref); i = 1 <= ref ? ++j : --j) {
results.push(new Confetti());
}
return results;
})();

window.step = function() {
var c, j, len, results;
requestAnimationFrame(step);
context.clearRect(0, 0, w, h);
results = /];
for (j = 0, len = confetti.length; j < len; j++) {
c = confettifj];
results.push(c.draw());
}
return results;
};

step();

}).call(this);

//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiPGFub255bW91cz4iXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7QUFBQSxNQUFBLE1BQUEsRUFBQSxRQUFBLEVBQUEsWUFBQSxFQUFBLElBQUEsRUFBQSxNQUFBLEVBQUEsUUFBQSxFQUFBLE9BQUEsRUFBQSxVQUFBLEVBQUEsQ0FBQSxFQUFBLEtBQUEsRUFBQSxZQUFBLEVBQUE7O0VBQUEsWUFBQSxHQUFlOztFQUNmLE1BQUEsR0FBUyxDQUFDLENBQUMsRUFBRCxFQUFJLEVBQUosRUFBTyxHQUFQLENBQUQsRUFBYyxDQUFDLEdBQUQsRUFBSyxFQUFMLEVBQVEsRUFBUixDQUFkLEVBQTJCLENBQUMsR0FBRCxFQUFLLEVBQUwsRUFBUSxFQUFSLENBQTNCLEVBQXdDLENBQ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