HTML5 canvas熱點圖應用,html5canvas熱點圖

來源:互聯網
上載者:User

HTML5 canvas熱點圖應用,html5canvas熱點圖

使用canvas API可以建立多種應用,下面我做一個我特別感興趣的例子,叫做熱點圖。

怎麼理解熱點圖呢,其實很簡單,就是度量溫度的意思,可以用於任何可測量的活動。介面上活躍高的部分以用亮色標記,活躍低的用暗色標記。舉個例子,熱點圖可以用在城市地表徵圖記交通路況,或者在世界各地地圖上上顯示風暴的活動情況。

接下來這個例子是,滑鼠移動到某個地區,會使某個地區的熱度增加。如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>heatmap</title>
        <style type="text/css">
            #heatmap{
                background-image:url(img/bac.jpg);
            }
        </style>
    </head>
    <body>
        <h1>heatMap</h1>
        <canvas id="heatmap" width="300" height="400"></canvas>
        <button id='button'>清空畫面</button>
    </body>
    <script type="text/javascript">
            
            function log() {
                    console.log(arguments);
                }
            
                var points = {};
                var SCALE = 3;
            
                var x = -1;
                var y = -1;
            
                function loadDemo() {
                    document.getElementById("button").onclick = reset;
            
                    canvas = document.getElementById("heatmap");
                    context = canvas.getContext('2d');
                    context.globalAlpha = 0.2;
                    context.globalCompositeOperation = "lighter";
            
                    function sample() {
                        if (x != -1) {
                            addToPoint(x,y)
                        }
                        setTimeout(sample, 100);
                    }
            
                    canvas.onmousemove = function(e) {
                        x = e.clientX - e.target.offsetLeft;
                        y = e.clientY - e.target.offsetTop;
                        addToPoint(x,y)
                    }
            
                    sample();
                }
            
                function reset() {
                    points = {};
                    context.clearRect(0,0,300,400);
                    x = -1;
                    y = -1;
                }
            
            
                function getColor(intensity) {
                    var colors = ['#2F4F4F','#008000','#228B22','#32CD32','#00FF00','#7CFC00','#ADFF2F','#90EE90','yellow'];
                    return colors[Math.floor(intensity/2)];
                }
            
                function drawPoint(x, y, radius) {
                        context.fillStyle= getColor(radius);
                        radius = Math.sqrt(radius)*6;
            
                        context.beginPath();
                        context.arc(x, y, radius, 0, Math.PI*2, true)
            
                        context.closePath();
                        context.fill();
                }
            
                function addToPoint(x, y) {
                    x = Math.floor(x/SCALE);
                    y= Math.floor(y/SCALE);
            
                    if (!points[[x,y]]) {
                        points[[x,y]] = 1;
                    } else if (points[[x,y]]==10) {
            return
                    } else {
                        points[[x,y]]++;
                    }
                    drawPoint(x*SCALE,y*SCALE, points[[x,y]]);
                }
            
            
                window.addEventListener("load", loadDemo, true);
                    
        
        
    </script>
</html>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.