【问题标题】:2 canvas using the drawImage to center one canvas within the other canvas2 画布使用 drawImage 将一个画布居中在另一个画布中
【发布时间】:2019-02-06 11:43:44
【问题描述】:

我试图在另一个画布上居中并将其居中。而且我希望它具有响应性,事实证明这比我想象的要难。

我有这个结构。

 <div class="photobooth"> // container
<div class="controls">    // just for the CTA btn
  <button class="cta" onClick="takePhoto()">Take Photo of your ID front please. Place the document  within the red border</button>
  <canvas class="blurPhoto" ></canvas> // the big canvas

// 小画布,用户应将文档放置在该画布的边框内 // 隐藏,这只是获取画布数据所必需的

<div class="strip">This goes to the database</div>

我想要的是一个应用了模糊的全屏背景。在那个模糊画布的中心(或者它可以是视频元素,也许对视频元素应用模糊并将画布居中会更好?),我想要一个清晰的画布,显示准实时的数据馈送在视频元素上。

内部画布的模糊背景和红色边框用作指示用户放置文档的位置(整个应用程序是 Revolut KYC UI 的原始模拟器)。

现在我不确定什么是最好的,用 CSS 或 JS 来设置这一切。

video {
position:absolute;
top:0px;
z-index:-1;
width:10%;
height:10%;
filter :blur(1px);
display:none;
}

我在这里隐藏视频,设置高度和宽度的百分比似乎并不重要,无论如何,Canvas 上的 JS 都会覆盖它。

由于我无法嵌套 Canvas,所以在这里我不知道还能做什么..

.photo { // this is the small canvas which should be centered within the big one
 position:absolute;

 width:50vw;
 height:50vh;

 border:1px red solid;


}

.blurPhoto { big canvas
 position:relative;
 width:100vw;
 height:100vh;
 filter :blur(10px);
  border:1px red solid;

 }

现在由于 .photo 画布父元素是包装 div,设置位置 top:100% 并没有达到我想要的效果,所以我将其删除。

这里我缓存了 2 个画布:

const canvas = document.querySelector('.photo');
const ctx = canvas.getContext('2d');
const canvasBig = document.querySelector('.blurPhoto');
const ctx2 = canvasBig.getContext('2d');

这里是相关的JS函数:

function paintToCanvas() {
const width = video.videoWidth;
const height = video.videoHeight;
canvas.width = width;
canvas.height = height;


return setInterval(() => {


 ctx.drawImage(video, 0,
    0, width, height);

 ctx2.drawImage(video, 0,
    0, width, height);  // this is overruled by the CSS set on it


 }, 16);
  }

那么,我该如何让它发挥作用。 我应该简单地使用视频元素作为背景吗? 有没有可能有这样的响应?

这里是codepen链接,我改了一些CSS,还是不满意。

https://codepen.io/damPop/pen/GwqxvM

【问题讨论】:

    标签: javascript css html canvas html5-video


    【解决方案1】:

    取自https://css-tricks.com/centering-css-complete-guide/

    将两个画布放在一个容器 div 中并使用 % 技巧来居中

    .container {
        position: relative;
    }
    .photo   {
        position: absolute;
        width:50vw;
        height:50vh;
        border:1px red solid;
    
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    .blurPhoto { big canvas
        position:absolute;
        top: 0;
        left: 0;
        width:100vw;
        height:100vh;
        filter :blur(10px);
        border:1px red solid;
     }
    

    【讨论】:

    • 由于各种原因,这确实不起作用。这不是一个简单的 CSS 居中问题。
    • 您正在使 blurPhoto 与视图一样大,但您是以相对方式定位它。你想让它覆盖整个窗口吗?如果你是,它需要是绝对的或固定的,或者你不能在页面上拥有任何其他东西,但它和它的后代。我不完全了解您需要对照片做什么,如果您已经了解这一点,我很抱歉,但是 css 中的定位几乎总是相对于父级或绝对值,因此您要么需要其父级有一个与您想要照片的任何位置保持一致的相对大小和位置,或者您必须手动定位照片。
    • 是的,模糊的照片应该占据屏幕的大部分,或者全部。这只是为了让用户有非模糊部分来放置他的文档。然后一些模糊的部分用作按钮的背景。去codepen,东西工作,然后你会看到我在那里尝试什么。它只是不是很令人满意而且没有响应。
    • 是的,我看到两个红框。如果我从中删除模糊,我可以看到 blurPhoto 框位于照片框的中心。在这种情况下,响应对您意味着什么?当我想到响应时,大多数时候它似乎被用来表示快速响应变化。似乎会减慢 UI 更改速度的东西是 * {transition:.5s;} ,它会使对 css 属性的每次更改减慢半秒。删除它可以让一切快速运行,但如果添加视频会减慢速度。
    • 啊,诺诺,我所说的响应式是指所有设备上的响应式画布,UI 的速度对我来说出奇的好。:-)
    猜你喜欢
    • 2015-08-12
    • 2012-07-17
    • 1970-01-01
    • 2015-02-15
    • 2011-04-27
    • 2017-01-28
    • 2013-12-26
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多