簡體   English   中英

清除 HTML 頁面,沒有 canvas

[英]Clear HTML page without a canvas

我想知道是否有任何方法可以在不使用 canvas 的情況下清除 HTML 頁面。

我正在嘗試使用不帶 canvas 的 JavaScript 制作一個簡單的程序,它從 window 的中心到鼠標指向的任何地方畫一條線。 無論何時何地鼠標移動,我都可以成功地繪制一條新線,但不知道如何在不制作 canvas 和使用clearRect()的情況下清除頁面。

沒有 canvas 有沒有辦法清除頁面?

以防萬一有人發現它有幫助,這是我的代碼:

      window.addEventListener('mousemove', function (e){
      linedraw(window.innerWidth/2, window.innerHeight/2, e.x, e.y)
      });

      function linedraw(x1, y1, x2, y2) {
    if (x2 < x1) {
        tmp = x2 ; x2 = x1 ; x1 = tmp
        tmp = y2 ; y2 = y1 ; y1 = tmp
    }

    lineLength = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    m = (y2 - y1) / (x2 - x1)

    degree = Math.atan(m) * 180 / Math.PI

    document.body.innerHTML += "<div class='line' style='transform-origin: top left; transform: rotate(" + degree + "deg); width: " + lineLength + "px; height: 1px; background: black; position: absolute; top: " + y1 + "px; left: " + x1 + "px;'></div>"
}

無需附加新的div ,只需在每次鼠標移動時替換 html 即可。

 window.addEventListener('mousemove', function (e){ linedraw(window.innerWidth/2, window.innerHeight/2, ex, ey) }); function linedraw(x1, y1, x2, y2) { if (x2 < x1) { tmp = x2; x2 = x1; x1 = tmp tmp = y2; y2 = y1; y1 = tmp } lineLength = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); m = (y2 - y1) / (x2 - x1) degree = Math.atan(m) * 180 / Math.PI // `document.body.innerHTML = ` instead of `document.body.innerHTML += ` document.body.innerHTML = "<div class='line' style='transform-origin: top left; transform: rotate(" + degree + "deg); width: " + lineLength + "px; height: 1px; background: black; position: absolute; top: " + y1 + "px; left: " + x1 + "px;'></div>" }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM