我发现了一个类似的问题,但它似乎没有回答我的问题。我有一个HTML5画布,我使用pubnc与webrtc。我想要做的是在画布之间传递我的图像,就像这里的数据传递http://pubnub.github.io/codoodler/history.html一样。有任何想法吗? (如果可能,请在答案中添加一个例子)
! 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 = []
}();
答案 0 :(得分:0)
您可以在(x,y)中发布/订阅画布坐标数据,就像代码示例一样,或者传递图像的url / path,但不能传递图像本身。
使用PubNub API可以publish
的每个数据的大小是<32k,因此即使使用{{1}将画布图像转换为base64,使用PubNub直接传递图像也不是理想的方式}。它仍然太大了。
您可能希望将画布图像数据发送到其他位置,例如Parse,然后只传递网址。