У меня есть пара видео, которые воспроизводятся на холстах с продолжением отсечения. Время от времени я просто вижу черный для видео, открепленный и не обработанный, и страница должна быть обновлена. Кто-нибудь знает, почему это может быть?
Кажется, что он работает, но я также слышу, как видеокарта постепенно становится громче. Поможет ли это как-то замедлить функцию requestAnimationFrame
? Все это было очень экспериментально, и я мог бы сделать это с дополнительной парой глаз.
Вот мой исходный код и спасибо заранее.
function makeLeftCanvasPath(ctx, width, height, widthFifteenPercent) {
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(width, 0);
ctx.lineTo(width - widthFifteenPercent, height);
ctx.lineTo(0, height);
ctx.lineTo(0, 0);
ctx.closePath();
}
function makeRightCanvasPath(ctx, width, height, widthFifteenPercent) {
ctx.beginPath();
ctx.moveTo(widthFifteenPercent, 0);
ctx.lineTo(width, 0);
ctx.lineTo(width, height);
ctx.lineTo(0, height);
ctx.lineTo(widthFifteenPercent, 0);
ctx.closePath();
}
var VideoSplit = function(container, sourceA, sourceB) {
var leftMediaSource = sourceA;
var leftMuted = true;
var leftCanvas = document.createElement('canvas');
leftCanvas.id = 'leftCanvas';
var ctxLeft = leftCanvas.getContext("2d");
var leftVideoContainer;
var leftVideo = document.createElement("video");
leftVideo.src = leftMediaSource;
leftVideo.autoplay = false;
leftVideo.loop = true;
leftVideo.muted = leftMuted;
leftVideoContainer = {
leftVideo: leftVideo,
ready: false,
ctx: ctxLeft
};
leftVideo.oncanplay = readyToPlayVideoLeft;
var rightMediaSource = sourceB;
var rightMuted = true;
var rightCanvas = document.createElement('canvas');
rightCanvas.id = 'rightCanvas';
var ctxRight = rightCanvas.getContext("2d");
var rightVideoContainer;
var rightVideo = document.createElement("video");
rightVideo.src = rightMediaSource;
rightVideo.autoplay = false;
rightVideo.loop = true;
rightVideo.muted = rightMuted;
$(rightVideo).attr('playsinline', '');
rightVideoContainer = {
leftVideo: rightVideo,
ready: false,
ctx: ctxRight
};
rightVideo.oncanplay = readyToPlayVideoRight;
// Create an overlay to capture mouse events
var eventOverlay = document.createElement('div');
eventOverlay.classList.add('eventOverlay');
container.appendChild(leftCanvas);
container.appendChild(rightCanvas);
container.appendChild(eventOverlay);
// Syncronise dimensions:
leftCanvas.width = leftCanvas.clientWidth;
rightCanvas.width = rightCanvas.clientWidth;
leftCanvas.height = leftCanvas.clientHeight;
rightCanvas.height = rightCanvas.clientHeight;
syncroniseCoordinateSystems();
ctxLeft.save();
ctxRight.save();
function syncroniseCoordinateSystems() {
// Syncronise dimensions:
var containerWidth = window.getComputedStyle(container).width;
container.style.height = (parseInt(containerWidth) / 100 * 56.25) / 2 + 'px';
var height = parseInt(container.style.height);
// Get 54.5% of the container width
var percentageOfWidth = parseInt(containerWidth) / 100 * 54.5;
leftCanvas.width = percentageOfWidth;
rightCanvas.width = percentageOfWidth;
leftCanvas.height = height;
rightCanvas.height = height;
leftCanvas.style.height = leftCanvas.height + 'px';
rightCanvas.style.height = rightCanvas.height + 'px';
var fifteenPercentOfWidth = rightCanvas.width / 100 * 15;
makeLeftCanvasPath(ctxLeft, leftCanvas.width, leftCanvas.height, fifteenPercentOfWidth);
ctxLeft.clip();
makeRightCanvasPath(ctxRight, ctxRight.canvas.width, ctxRight.canvas.height, fifteenPercentOfWidth);
ctxRight.clip();
}
function readyToPlayVideoRight(event) {
rightVideoContainer.ready = true;
}
function readyToPlayVideoLeft(event) {
leftVideoContainer.ready = true;
requestAnimationFrame(updateCanvasLeft);
}
function updateCanvasLeft() {
var containerWidth = window.getComputedStyle(container).width;
// Set the style of the actual container so it retains video aspect ratio
container.style.height = (parseFloat(containerWidth) / 100 * 56.25) / 2 + 'px';
var height = parseFloat(container.style.height);
var width = parseInt(containerWidth) / 100 * 54.5;
// only draw if loaded and ready
if (leftVideoContainer !== undefined && leftVideoContainer.ready && rightVideoContainer !== undefined && rightVideoContainer.ready) {
ctxLeft.drawImage(leftVideoContainer.leftVideo, 0, 0, width, height);
ctxRight.drawImage(rightVideoContainer.leftVideo, 0, 0, width, height);
}
requestAnimationFrame(updateCanvasLeft);
}
window.addEventListener('resize', function() {
syncroniseCoordinateSystems();
var fifteenPercentOfWidth = leftCanvas.width / 100 * 15;
makeLeftCanvasPath(ctxLeft, leftCanvas.width, leftCanvas.height, fifteenPercentOfWidth);
ctxLeft.clip();
makeRightCanvasPath(ctxRight, ctxRight.canvas.width, ctxRight.canvas.height, fifteenPercentOfWidth);
ctxRight.clip();
});
function playPauseClick(canvasClicked) {
var canvasClickedId = canvasClicked.id;
var videoContainer = (canvasClickedId == 'leftCanvas' ? leftVideoContainer : rightVideoContainer);
if (videoContainer !== undefined && videoContainer.ready) {
if (videoContainer.leftVideo.paused) {
videoContainer.leftVideo.play();
}
}
}
eventOverlay.addEventListener('mousemove', function(e) {
var rect = this.getBoundingClientRect();
var x = e.clientX - rect.left;
var y = e.clientY - rect.top;
if (ctxLeft.isPointInPath(x, y)) {
leftVideo.play();
rightVideo.pause();
} else {
leftVideo.pause();
rightVideo.play();
}
});
eventOverlay.addEventListener('mouseout', function(e) {
leftVideo.pause();
rightVideo.pause();
});
};