ホームページ ウェブフロントエンド H5 チュートリアル Canvasを使ってロードマップを描く方法(コード)

Canvasを使ってロードマップを描く方法(コード)

Aug 15, 2018 pm 05:02 PM

この記事の内容は、Canvas を使用してロードマップ (コード) を描画する方法に関するものです。必要な方は参考にしていただければ幸いです。

<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta content="always" name="referrer">
    <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0" name="viewport" />
    <title>CITEK反向寻车</title>
    <script src="<%=basePath%>wui/js/jquery.js"></script>
    <link rel="stylesheet" href="<%=cssPath%>wui.css" type="text/css"></link>
    <script type="text/javascript" src="<%=basePath%>wui/js/line_tool.js"></script>
    <script type="text/javascript">
var arrPosX = [], arrPosY = [];
        <s:iterator value="listNode" status="bean">          //设置路线中点的横坐标和纵坐标的集合
            arrPosX.push(<s:property value="posX" />);
            arrPosY.push(<s:property value="posY" />);
        </s:iterator>
        var arrRoundPosX = [], arrRoundPosY = [];          //设置终点所在区域的范围点横坐标和纵坐标集合
        <s:iterator value="positionsX" status="bean">
            arrRoundPosX.push(<s:property />);
        </s:iterator>
        <s:iterator value="positionsY" status="bean">
            arrRoundPosY.push(<s:property />);
        </s:iterator>

        var ctxBackground, canvasBackground;
           var ctxSource, canvasSource;
        var canvasWidth, canvasHeight;
        
        var imgStart, imgEnd, imgBackground,;
        var areaImage;
        var isStart = false;
        
        var scale = 1;
        var scaleInterval = 3;
        var scaleCount = 0;
        var runCount = 0;
        var step = 2;    //像素
        var moveX = 1;
        var moveY = 1;
        var currIndex = 0;
        var a = 0;
        var tmpIconPaths = [                                                         //设置起点图标
            "<%=basePath%>img/point_start.png", 
        ];
        var imgObjArr = [];
        var iLoadIndex = 0;
        
        /**
         * 将图标放入集合中
         */
        function loadIconImages(){
            var oImg = new Image();
            oImg.addEventListener(&#39;load&#39;, eventIconImagesLoaded, false);
            oImg.src = tmpIconPaths[iLoadIndex];
            imgObjArr.push(oImg);
        }
        
        /**
         * 加载图标
         */
        function eventIconImagesLoaded(){
            iLoadIndex++;
            if(iLoadIndex <= 3) {
                loadIconImages();
            } else {
                loadImage();
            }
        }
        
        /**
         * 加载背景图标
         */
        function loadImage(){
               areaImage = new Image();
               areaImage.addEventListener(&#39;load&#39;, eventAreaImageLoaded, false);
               areaImage.src ="<%=basePath%>image/img.jpg;
        }
        
        function eventAreaImageLoaded(){
            initBase();
            initScene();
            initSprits();
            start();
            isStart = true;
        }
        
        /**
         * 初始化
         */
        function initBase() {
            imgStart = imgObjArr[0];
    
            canvasBackground = document.getElementById("canvasBackground");
            ctxBackground = canvasBackground.getContext("2d");
    
            canvasSource = document.getElementById("canvasSource");
            ctxSource = canvasSource.getContext("2d");
            
            canvasWidth = areaImage.width;
            canvasHeight = areaImage.height;
            
            var bodyWidth = document.body.clientWidth-10;
            var bodyHeight = document.body.clientHeight-10;
            var tmpCavW = canvasWidth;
            var tmpCavH = canvasHeight;
            
            if(canvasWidth > bodyWidth)    {
                canvasWidth = bodyWidth;
                canvasHeight = canvasWidth * (tmpCavH/tmpCavW);
            }
            if(canvasHeight > bodyHeight){
                canvasHeight = bodyHeight;
                canvasWidth = canvasHeight * (tmpCavW/tmpCavH);
            }
            canvasBackground.width = canvasWidth;
            canvasBackground.height = canvasHeight;
            
            canvasSource.width = canvasWidth;
            canvasSource.height = canvasHeight;
            moveX = arrPosX[0] * canvasWidth;
            moveY = arrPosY[0] * canvasHeight;
            
        }
        
        /**
         * 初始化画布
         */
        function initScene() {
            ctxBackground.drawImage(areaImage, 0, 0, canvasWidth, canvasHeight);
        }
        
        /**
         * 开始绘图
         */
        function initSprits() {
            /* 绘制路线的白底 */
             ctxBackground.beginPath();
             ctxBackground.strokeStyle = "white";
               ctxBackground.lineWidth = 8;
               ctxBackground.lineCap = "round";
               ctxBackground.lineJoin = "miter";
               ctxBackground.miterLimit = 30;
               for(var i=1; i < arrPosX.length; i++){
                   ctxBackground.moveTo(canvasWidth * arrPosX[i-1], canvasHeight * arrPosY[i-1]);   //指定一条线段的起点   
                   ctxBackground.lineTo(canvasWidth * arrPosX[i],     canvasHeight * arrPosY[i]);     //指定一条线段的终点   
             }  
             ctxBackground.stroke();
              /* 绘制路线的红线 */
             ctxBackground.beginPath();                                             //是通过覆盖白底实现的
               ctxBackground.strokeStyle = "rgba(255,0,0,1)";
               ctxBackground.lineWidth = 4;
               ctxBackground.lineCap = "round";
               ctxBackground.lineJoin = "miter";
               ctxBackground.miterLimit = 30;
               for(var i=1; i < arrPosX.length; i++){
                   ctxBackground.moveTo(canvasWidth * arrPosX[i-1], canvasHeight * arrPosY[i-1]);   //指定一条线段的起点   
                 ctxBackground.lineTo(canvasWidth * arrPosX[i],     canvasHeight * arrPosY[i]);     //指定一条线段的终点   
             }  
             ctxBackground.stroke();
             
            /* 绘制终点区域 */                                                                                     
            ctxSource.clearRect(0, 0, canvasWidth,canvasHeight);
            ctxBackground.beginPath();
            ctxBackground.strokeStyle = "rgba(255,0,0,1)";   //颜色
            ctxBackground.lineWidth = 0.5;
            ctxBackground.fillStyle = "rgba(255,0,0,0)";   //透明度
               ctxBackground.moveTo(canvasWidth * arrRoundPosX[0], canvasHeight * arrRoundPosY[0]);   //指定一条线段的起点   
             for(var i=1; i < arrRoundPosX.length; i++){
                 ctxBackground.lineTo(canvasWidth * arrRoundPosX[i],     canvasHeight * arrRoundPosY[i]);     //指定一条线段的终点 
             }   
            ctxBackground.lineTo(canvasWidth * arrRoundPosX[0],     canvasHeight * arrRoundPosY[0]);  
            ctxBackground.closePath();
            ctxBackground.fill();
            ctxBackground.stroke();

             /* 绘制起点图标 */
            ctxBackground.drawImage(
                    imgStart, 
                    canvasWidth * arrPosX[0] - imgStart.width * 0.25, 
                    canvasHeight * arrPosY[0] - imgStart.height * 0.25 - imgStart.height * 0.25, 
                    imgStart.width * 0.5, 
                    imgStart.height * 0.5);
        }
        
        /**
         * 设置图标的跳动
         */
        function loop(){
            if(!isStart) return;
            if(scale > 1.8) scale = 1;
            if(scaleCount > 999999) scaleCount = 0;
            if(runCount > 999999) runCount = 0;
            ctxSource.save();
            ctxSource.clearRect(0,0,canvasWidth,canvasHeight);
            /* 设置起点图标的跳动 */
            ctxSource.translate(
                    canvasWidth * arrPosX[0] - imgStart.width * 0.25 +imgStart.width*0.25,
                    canvasHeight * arrPosY[0] - imgStart.height * 0.25+imgStart.height*0.25);
            ctxSource.scale(scale, scale);
            ctxSource.shadowOffsetX = 3; // 阴影x轴偏移
            ctxSource.shadowOffsetY = 4; // 阴影y轴偏移
            ctxSource.shadowBlur = 2; // 模糊尺寸
            ctxSource.shadowColor = &#39;rgba(0, 0, 0, 0.5)&#39;; // 颜色    
            ctxSource.drawImage(
                    imgStart, 
                    - imgStart.width * 0.25, 
                    - imgStart.height * 0.25 - imgStart.height * 0.25, 
                    imgStart.width * 0.5, 
                    imgStart.height * 0.5);
            ctxSource.restore();
            ctxSource.save();
            
            
            /* 设置终点区域的透明度变化 */
            if (runCount % 4 == 0) {
                 a = a + 0.1;
            }
            if (a > 0.6) {
                a = 0;
            }
            ctxSource.strokeStyle = "rgba(255,0,0,1)";
            ctxSource.lineWidth = 10;
            ctxSource.fillStyle = "rgba(255,0,0,"+a+")";
               ctxSource.moveTo(canvasWidth * arrRoundPosX[0], canvasHeight * arrRoundPosY[0]);   //指定一条线段的起点   
             for(var i=1; i < arrRoundPosX.length; i++){
                 ctxSource.lineTo(canvasWidth * arrRoundPosX[i],     canvasHeight * arrRoundPosY[i]);     //指定一条线段的终点   
             }   
            ctxSource.lineTo(canvasWidth * arrRoundPosX[0],     canvasHeight * arrRoundPosY[0]);  
            ctxSource.closePath();
            ctxSource.fill();
            ctxSource.restore();
            ctxSource.save();
            
        
            scaleCount++;
            runCount++;
            if(scaleCount % scaleInterval == 0){
                scale += 0.1;
            }
        }
        
        /**
         * 设置标题和图片的长宽高和跳动频率
         */
        function start(){
            $("#monitor_list_box").width = canvasWidth + "px";
            $("#monitor_list_box").height = canvasHeight + "px";
            $("#canvasBackground").width = canvasWidth + "px";
            $("#canvasBackground").height = canvasHeight + "px";
            $("#canvasSource").width = canvasWidth + "px";
            $("#canvasSource").height = canvasHeight + "px";
            window.setInterval(loop,1000/30);    //60帧
        }
        
  </script>
  <style type="text/css">
        #monitor_list_box {width:100%; height:auto; overflow: auto;}
        #monitor_list_box canvas {position:absolute;width:100%; height:auto;}
 </style>
<body>
<div id="monitor_list_box">
        <canvas id="canvasBackground">
            Your browser does not support the canvas element.
        </canvas>
        <canvas id="canvasSource">
            Your browser does not support the canvas element.
        </canvas>
    </div>
</body>
ログイン後にコピー

関連おすすめ:

HTML5 Canvas - パスを使った線の描画例紹介_html5チュートリアルスキル

HTML5 シンプルで美しい回路図を描く

HTML5簡易オンライン描画ツール実装の詳細 Case

以上がCanvasを使ってロードマップを描く方法(コード)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

H5プロジェクトの実行方法 H5プロジェクトの実行方法 Apr 06, 2025 pm 12:21 PM

H5プロジェクトを実行するには、次の手順が必要です。Webサーバー、node.js、開発ツールなどの必要なツールのインストール。開発環境の構築、プロジェクトフォルダーの作成、プロジェクトの初期化、コードの書き込み。開発サーバーを起動し、コマンドラインを使用してコマンドを実行します。ブラウザでプロジェクトをプレビューし、開発サーバーURLを入力します。プロジェクトの公開、コードの最適化、プロジェクトの展開、Webサーバーの構成のセットアップ。

H5ページの生産とはどういう意味ですか? H5ページの生産とはどういう意味ですか? Apr 06, 2025 am 07:18 AM

H5ページの制作とは、HTML5、CSS3、JavaScriptなどのテクノロジーを使用したクロスプラットフォーム互換のWebページの作成を指します。そのコアは、ブラウザの解析コード、レンダリング構造、スタイル、インタラクティブ機能にあります。一般的なテクノロジーには、アニメーションエフェクト、レスポンシブデザイン、およびデータ相互作用が含まれます。エラーを回避するには、開発者をデバッグする必要があります。パフォーマンスの最適化とベストプラクティスには、画像形式の最適化、リクエスト削減、コード仕様などが含まれ、読み込み速度とコード品質を向上させます。

H5のクリックアイコンの作成方法 H5のクリックアイコンの作成方法 Apr 06, 2025 pm 12:15 PM

H5クリックアイコンを作成する手順には、次のものがあります。画像編集ソフトウェアで正方形のソース画像の準備が含まれます。 H5エディターにインタラクティブ性を追加し、クリックイベントを設定します。アイコン全体をカバーするホットスポットを作成します。ページにジャンプしたり、アニメーションのトリガーなど、クリックイベントのアクションを設定します。 HTML、CSS、およびJavaScriptファイルとしてH5ドキュメントをエクスポートします。エクスポートされたファイルをウェブサイトまたは他のプラットフォームに展開します。

H5は何を参照していますか?コンテキストの探索 H5は何を参照していますか?コンテキストの探索 Apr 12, 2025 am 12:03 AM

H5ReferStoHtml5、apivotaltechnologyinwebdevelopment.1)html5introduceSnewelementsandapisforrich、dynamicwebapplications.2)Itupp ortsmultimediawithoutplugins、endancingurexperiencecrossdevices.3)semanticelementsimprovecontentstructurendseo.4)H5'srespo

H5ページの生産に適したアプリケーションシナリオ H5ページの生産に適したアプリケーションシナリオ Apr 05, 2025 pm 11:36 PM

H5(HTML5)は、マーケティングキャンペーンページ、製品ディスプレイページ、企業プロモーションマイクロウェブサイトなどの軽量アプリケーションに適しています。その利点は、クロスプラットフォームと豊富な対話性にありますが、その制限は複雑な相互作用とアニメーション、ローカルリソースアクセス、オフライン機能にあります。

H5ページの生産はフロントエンド開発ですか? H5ページの生産はフロントエンド開発ですか? Apr 05, 2025 pm 11:42 PM

はい、H5ページの生産は、HTML、CSS、JavaScriptなどのコアテクノロジーを含むフロントエンド開発のための重要な実装方法です。開発者は、&lt; canvas&gt;の使用など、これらのテクノロジーを巧みに組み合わせることにより、動的で強力なH5ページを構築します。グラフィックを描画するタグまたはJavaScriptを使用して相互作用の動作を制御します。

H5プログラミング言語とは何ですか? H5プログラミング言語とは何ですか? Apr 03, 2025 am 12:16 AM

H5はスタンドアロンプ​​ログラミング言語ではなく、最新のWebアプリケーションを構築するためのHTML5、CSS3、およびJavaScriptのコレクションです。 1。HTML5は、Webページの構造とコンテンツを定義し、新しいタグとAPIを提供します。 2。CSS3はスタイルとレイアウトを制御し、アニメーションなどの新しい機能を紹介します。 3. JavaScriptは動的な相互作用を実装し、DOM操作と非同期要求を通じて機能を強化します。

H5でポップアップウィンドウの作り方 H5でポップアップウィンドウの作り方 Apr 06, 2025 pm 12:12 PM

H5ポップアップウィンドウの作成手順:1。トリガー方法(クリック、時間、終了、スクロール)を決定します。 2。設計コンテンツ(タイトル、テキスト、アクションボタン); 3。SETスタイル(サイズ、色、フォント、背景); 4.コードを実装する(HTML、CSS、JavaScript); 5。テストと展開。

See all articles