Skip to content

Instantly share code, notes, and snippets.

@sridharraman
Last active February 24, 2022 15:54
Show Gist options
  • Select an option

  • Save sridharraman/2d3dfd8b51f30a676ffcb5da3555a509 to your computer and use it in GitHub Desktop.

Select an option

Save sridharraman/2d3dfd8b51f30a676ffcb5da3555a509 to your computer and use it in GitHub Desktop.
Game to API
// import html2canvas from 'html2canvas';
// on button click
document.querySelector("#btnShare").addEventListener("click", (e) => {
// convert #preamble to canvas
html2canvas(document.querySelector("#preamble"), {
useCORS: true,
allowTaint: true
}).then(canvas => {
// get url
const imgData = canvas.toDataURL('image/png');
// send to api
console.log('sending to api ...');
const gameId = 'ABCD';
const now = new Date().toISOString();
console.log(now);
// preamble image
fetch(`https://frozen-forest-36963.herokuapp.com/save-image?gameId=${gameId}&ts=${now}`, {
method: 'POST',
headers: {
'Content-Type': 'text/plain'
},
body: imgData
}).then(function(res) {
res.text().then(function (text) {
console.log(text)
})
}).catch(function(err) {
console.log(err);
});
// game params
// sample below:
const analyticsObject = {
scenario: 3,
choices: [1, 4, 5],
preambleStats: [1, 3, 4, 5, 7, 3, 2]
}
fetch(`https://frozen-forest-36963.herokuapp.com/save-parameters?gameId=${gameId}&ts=${now}`, {
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(analyticsObject)
}).then(function(res) {
console.log(res.status)
}).catch(function(err) {
console.log(err)
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment