1

キャンバスに多数の画像と 4 つの「説明ボックス」を表示するために使用している HTML5 キャンバスを含む Web ページがあります。

意図は、ユーザーが画像を対応する説明ボックスにドラッグ アンド ドロップできるようにすることですが、ドラッグ アンド ドロップを機能させるのに少し苦労しています。

画像のドラッグ アンド ドロップを有効にする方法をいくつか試しましたが、どれも機能していないようです。

私が抱えている問題は、キャンバスに表示される内容が動的である必要があることです。つまり、管理者は、キャンバスで使用される画像のファイル名を変更することで、キャンバスに表示される画像などを変更できます。 XML ファイルを、彼がむしろ使用したい画像に変換します。

基本的には、キャンバスに画像を表示する JavaScript にコードを追加して、キャンバス上で画像をドラッグ アンド ドロップできるようにしたいと考えています。

これを行うための最良の方法は何か提案はありますか?

私の HTML ページは次のようになります。

<!DOCTYPE html>
<html>
<head>
<script src = "kinetic.js" type = "text/javascript"></script>
<title>Home</title>

<script src = "drawLevelOneElements.js" type = "text/javascript"></script>
<script src = "layers&analytics.js" type = "text/javascript"></script>
<script src = "startGameDrawGameElementsDrawStartButton.js" type = "text/javascript"></script>
<script src = "interaction.js" type = "text/javascript"></script>
<script src = "dragAndDrop.js" type = "text/javascript"></script>
<script src = "dragAndDrop1.js" type = "text/javascript"></script>
<script src = "dragAndDrop2.js" type = "text/javascript"></script>
<script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.0.5.js"></script>


</head>

<body onLoad="startGame()">

<section hidden>
<img id="StartButton" src="StartButton.png" alt="Start Button" width="179" height="180" href="javascript:drawLevelOneElements();"/>
</section>

    <h1>Home</h1>
    <p>The purpose of this website is to teach users the basic principles of running a business by playing the game below. <br /><br /></p>

    <canvas id="gameCanvas" width="1000" height="500" style="border:1px solid">
    Your browser does not support the canvas element.
    </canvas>

    <br /><br />
    <p>Use this paragraph to enter text that provides the user with instructions for how to play the game. <br />
        Update the instructions so that they're appropriate to whatever level the user is currently playing.</p>

    <script src = "variables&preloadingImages.js" type = "text/javascript"></script>
</body>

ドラッグ アンド ドロップ機能を 3 つの異なる方法で追加しようとしました。したがって、3 つのドラッグ アンド ドロップ JS ファイルを含めましたが、どれも機能していないようです。

キャンバスに画像を描画するために使用している JavaScript は、各要素が 1 つの画像を保持する単純な配列です。キャンバスに描画している画像のカテゴリごとに配列があり、各カテゴリの画像の数は可変です。

次に、JS concat メソッドを使用して配列を連結し、すべての画像を という 1 つの配列に入れましたallImagesArray

キャンバス上のランダムな場所に allImagesArray から各画像を描画する次の関数があります。

function drawLevelOneElements(){
            /*First, clear the canvas */ 
            context.clearRect(0, 0, myGameCanvas.width, myGameCanvas.height);
            /*This line clears all of the elements that were previously drawn on the canvas. */
            /*Then redraw the game elements */
            drawGameElements(); 

            /*Create the four description areas, and place them near the bottom of the canvas */
            /*Create boxes with rounded corners for the description areas */
            CanvasRenderingContext2D.prototype.drawDescriptionArea = function(x, y, width, height, radius, stroke){
                if(typeof stroke == "undefined" ){
                    stroke = true;
                }
                if(typeof radius === "undefined"){
                    radius = 5;
                }
                this.beginPath();
                this.moveTo(x + radius, y);
                this.lineTo(x + width - radius, y);
                this.quadraticCurveTo(x + width, y, x + width, y + radius);
                this.lineTo(x + width, y + height - radius);
                this.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
                this.lineTo(x + radius, y + height);
                this.quadraticCurveTo(x, y + height, x, y + height - radius);
                this.lineTo(x, y + radius);
                this.quadraticCurveTo(x, y, x + radius, y);
                this.closePath();
                if(stroke){
                    context.stroke();
                }
            }

            context.drawDescriptionArea(70, 400, 120, 70);
            context.font = '25pt Calibri';
            context.strokeText('Asset', 90, 440);

            context.drawDescriptionArea(300, 400, 120, 70);
            context.strokeText('Liability', 310, 440);

            context.drawDescriptionArea(540, 400, 120, 70);
            context.strokeText('Income', 550, 440);

            context.drawDescriptionArea(750, 400, 180, 70);
            context.strokeText('Expenditure', 760, 440);

            /*Now draw the images to the canvas */
            /*First, create variables for the x & y coordinates of the image that will be drawn.
                the x & y coordinates should hold random numbers, so that the images will be 
                drawn in random locations on the canvas.*/
                var imageX = Math.floor(Math.random()*100);
                var imageY = Math.floor(Math.random()*100);

                /*Create a 'table' of positions that the images will be drawn to */
                var imagePositionsX = [20, 80, 140, 200, 260, 320, 380, 440, 500, 560];
                var imagePositionsY = [20, 60, 100, 140, 180, 220, 260, 300, 340, 380];

            /*Draw all images from assetsImageArray */
            /*Use a while loop to loop through the array, get each item and draw it. */
            var arrayIteration = 0;
            console.log('All Images Array length: ' + allImagesArray.length); /*Display the length of the array in the console, to check it's holding the correct number of images. */
            while(arrayIteration < allImagesArray.length){
                var randomPositionX = Math.floor(Math.random()*10);
                var randomPositionY = Math.floor(Math.random()*10);
                context.drawImage(allImagesArray[arrayIteration], imageX, imageY, 50, 50);
                console.log(arrayIteration); /*Display the current array position that's being drawn */
                arrayIteration = arrayIteration+1;
                /*Now try changing the values of imageX & imageY so that the next image is drawn to a 
                    different location*/
                imageX = imagePositionsX[randomPositionX];  /* imageX+(Math.floor(Math.random()*100)); */
                imageY = imagePositionsY[randomPositionY];  /* imageY+(Math.floor(Math.random()*100));  */

            }

        }

この関数は、キャンバス上に「資産」、「負債」、「収入」、「支出」というラベルの付いた 4 つの「説明領域」も描画します。

ユーザーが行う必要があるのは、各画像を対応する「説明領域」にドラッグすることです。画像を正しい説明領域にドラッグすると、キャンバスから消えるはずです。

しかし、キャンバスに画像と説明領域を描画しましたが、画像にドラッグ アンド ドロップ機能を追加する方法がわかりません。

私はいくつかの例をオンラインで見てきましたが、それらのどれも機能していないようです.whileループを使用して配列から画像を描画しているからでしょうか? このサイトで尋ねられた他の質問も見ましたが、それらのいずれにも答えが見つからないようです.

http://www.html5canvastutorials.com/kineticjs/html5-canvas-drag-and-drop-an-image-tutorial/およびhttp://www.html5canvastutorials.com/labs/html5のチュートリアルを見てきました-canvas-drag-and-drop-multiple-images-with-kineticjs/しかし、これらの例を私のコードで動作させることができないようです。関数を使用してキャンバスに描画された各画像にドラッグ アンド ドロップ機能を追加する方法について、誰かアイデアはありますdrawLevelOneElements()か?

4

1 に答える 1

0

その場合、cgSceneGraphは簡単に役立ちます。サンプルページを見ることができます(ソースコードはデモに含まれています):http: //gwennaelbuchet.github.com/cgSceneGraph/examples.html

私はこのフレームワークの設計者なので、プロジェクトに含めるのを簡単にお手伝いできます:)

オブジェクトをドラッグ可能にするには、次のようにします。

this.img = new CGSGNodeImage(x, y, sourceURL);
this.img.isDraggable = true;
this.img.isCollisionManaged = true;

次に、画像が説明領域内にあるかどうかを知るために、次のようにcgSceneGraphの衝突検出を使用できます。

//detect collision each frame
var bindCheckCollision = this.checkCollision.bind(this);
this.onRenderEnd = function () {
    bindCheckCollision();
};

およびcheckCOllisionメソッド:

checkCollision: function () {
    var isColliding = this.img.isColliding(this.descriptionNode, 0);
}

これ以上することはありません:)

完全な例はウェブサイトにあります。これがお役に立てば幸いです。

グウェン。

于 2013-02-10T10:56:07.987 に答える