Refactor to transform the canvas image into a url data

This commit is contained in:
Angela Mazzurco 2017-07-26 19:07:28 +02:00
parent d1884c3b86
commit f578833098

View File

@ -1,19 +1,32 @@
import QtQuick 2.7 import QtQuick 2.7
import Qt.labs.settings 1.0
import "../" // Needed by ChatCache (where stores generated faces)
Item Item
{ {
id: faceRoot id: faces
property string hash property string hash
property var facesCache: ChatCache.facesCache
Image
{
id: faceAvatar
width: iconSize
height: iconSize
}
Canvas Canvas
{ {
id: faceCanvas id: faceCanvas
width: iconSize width: iconSize
height: iconSize height: iconSize
visible: false
property var images property var images
property var callback
onPaint: onPaint:
@ -27,6 +40,13 @@ Item
ctx.drawImage(images[y], 0, 0, iconSize, iconSize ) ctx.drawImage(images[y], 0, 0, iconSize, iconSize )
} }
} }
if (callback)
{
var data = faceCanvas.toDataURL()
callback(data)
}
} }
} }
@ -36,9 +56,6 @@ Item
} }
property var iconCache: ({})
property var callbackCache: ({})
property var facesPath: "/icons/faces/" property var facesPath: "/icons/faces/"
property var iconSize: 32 property var iconSize: 32
@ -108,42 +125,45 @@ Item
var onloads = []; var onloads = [];
for (var i=0; i<nPieces; i++) for (var i=0; i<nPieces; i++)
{ {
onloads.push(src(gender, i, data[i+1])) var url = src(gender, i, data[i+1])
faceCanvas.loadImage(src(gender, i, data[i+1])) onloads.push(url)
faceCanvas.loadImage(url)
} }
faceCanvas.images = onloads faceCanvas.images = onloads
faceCanvas.callback = callback
faceCanvas.requestPaint() faceCanvas.requestPaint()
} }
// Create the identicon // Create the identicon
function createFromHex(dataHex) function createFromHex(dataHex)
{ {
var iconId = [dataHex, iconSize]; var iconId = [dataHex, iconSize];
var update = function(data) var update = function(data)
{ {
iconCache[iconId] = data; facesCache.iconCache[iconId] = data;
// element.html('<img class="identicon" width='+iconSize+' height='+iconSize+' src="'+data+'">'); faceAvatar.source = data
} }
if (iconCache.hasOwnProperty(iconId))
if (facesCache.iconCache.hasOwnProperty(iconId))
{ {
update(iconCache[iconId]); update(facesCache.iconCache[iconId])
} }
else if(callbackCache.hasOwnProperty(iconId)) else if(facesCache.callbackCache.hasOwnProperty(iconId))
{ {
callbackCache[iconId].push(update); facesCache.callbackCache[iconId].push(update)
} }
else else
{ {
var onImageGenerated = function(data) var onImageGenerated = function(data)
{ {
callbackCache[iconId].forEach(function(callback)
facesCache.callbackCache[iconId].forEach(function(callback)
{ {
callback(data); callback(data);
}) })
} }
callbackCache[iconId] = [update]; facesCache.callbackCache[iconId] = [update];
if (dataHex) if (dataHex)
{ {
generateImage(calcDataFromFingerprint(dataHex), onImageGenerated); generateImage(calcDataFromFingerprint(dataHex), onImageGenerated);