I have found a similar question but it doesn't seem to answer my question. I have an HTML5 canvas and i use pubnub with webrtc. What i want to do is to pass my image between the canvas like the data from here are passed http://pubnub.github.io/codoodler/history.html. Any ideas? (Please add an example to the answer if possible)
! function() {
function a(a) {
q.publish({
channel: 'mychannel12',
message: a
})
}
function b(a, b) {
h.strokeStyle = a, h.beginPath(), h.moveTo(b[0].x, b[0].y);
for (var c = 1; c < b.length; c++) h.lineTo(b[c].x, b[c].y);
h.stroke()
}
function c(a) {
!a || a.plots.length < 1 || b(a.color, a.plots)
}
function d(a) {
if (a.preventDefault(), r) {
var c = j ? a.targetTouches[0].pageX - g.offsetLeft : a.offsetX || a.layerX - g.offsetLeft,
d = j ? a.targetTouches[0].pageY - g.offsetTop : a.offsetY || a.layerY - g.offsetTop;
s.push({
x: c << 0,
y: d << 0
}), b(i, s)
}
}
function e(a) {
a.preventDefault(), r = !0
}
function f(b) {
b.preventDefault(), r = !1, a({
color: i,
plots: s
}), s = []
}
var g = document.getElementById("drawCanvas"),
h = g.getContext("2d"),
i = document.querySelector(":checked").getAttribute("data-color");
g.width = Math.min(document.documentElement.clientWidth, window.innerWidth || 300), g.height = Math.min(document.documentElement.clientHeight, window.innerHeight || 300), h.strokeStyle = i, h.lineWidth = "3", h.lineCap = h.lineJoin = "round", document.getElementById("colorSwatch").addEventListener("click", function() {
i = document.querySelector(":checked").getAttribute("data-color")
}, !1);
var j = "ontouchstart" in window,
k = navigator.pointerEnabled,
l = navigator.msPointerEnabled,
m = j ? "touchstart" : k ? "pointerdown" : l ? "MSPointerDown" : "mousedown",
n = j ? "touchmove" : k ? "pointermove" : l ? "MSPointerMove" : "mousemove",
o = j ? "touchend" : k ? "pointerup" : l ? "MSPointerUp" : "mouseup";
g.addEventListener(m, e, !1), g.addEventListener(n, d, !1), g.addEventListener(o, f, !1);
var p = "draw",
q = PUBNUB.init({
publish_key: "pub-c-465c4b3b-0b7d-40de-86c5-10a9433058b5",
subscribe_key: "sub-c-43a257e0-d94a-11e4-a2b8-0619f8945a4f",
leave_on_unload: !0
});
q.subscribe({
channel: 'mychannel12',
callback: c,
presence: function(a) {
a.occupancy > 1 && (document.getElementById("unit").textContent = "doodlers"), document.getElementById("occupancy").textContent = a.occupancy;
var b = document.getElementById("occupancy").parentNode;
b.classList.add("anim"), b.addEventListener("transitionend", function() {
b.classList.remove("anim")
}, !1)
}
}), drawHistory && q.history({
channel: 'mychannel12',
count: 50,
callback: function(a) {
q.each(a[0], c)
}
});
var r = !1,
s = []
}();
You can publish/subscribe the canvas coordinates data in (x, y), just like the code sample, or pass the url/path of the image, but not the image itself.
The size of each data you can
publish
using PubNub APIs is <32k, so passing images directly using PubNub is not an ideal way, even if you convert a canvas image to a base64 withcanvas.toDataURL
. It would be still too large.You probably want to send the canvas images data elsewhere, like Parse, and just pass the url.