简体   繁体   English

我可以在画布中放一个HTML按钮吗?

[英]Can I put an HTML button inside the canvas?

I want to make the buttons for the game I'm making as real HTML buttons, but they need to be inside the canvas. 我想制作我正在制作的游戏按钮作为真正的HTML按钮,但它们需要在画布内。

How would I go about doing this? 我该怎么做呢?

Given that the canvas element has a transparent content model , it may contain fallback elements which are displayed in the event that the canvas element is unsupported. 假设canvas元素具有透明内容模型 ,它可能包含在不支持canvas元素的情况下显示的回退元素。 They will not be displayed if the canvas is supported. 如果支持画布, 不会显示它们。

You can position HTML elements relative to the canvas' parent to have the buttons "hovering" over the canvas. 您可以相对于画布的父级定位HTML元素,以使按钮“悬停”在画布上。 A menu element could be an appropriately semantic element to render a list of controls, depending on the context: 菜单元素可以是一个适当的语义元素,用于呈现控件列表,具体取决于上下文:

HTML: HTML:
 <div id="container"> <canvas id="viewport"> </canvas> <menu id="controls"> </menu> </div> 
CSS: CSS:
 #container { height: 400px; position: relative; width: 400px; } #viewport { height: 100%; width: 100%; } #controls { bottom: 0; left: 0; position: absolute; width: 100%; } 

You can put the button on top of the canvas by giving the canvas a z-index which is lower than the z-index of the button: 你可以把在顶部的按钮canvas通过给画布上z-index比低z-index按钮:

<canvas style="z-index:1"></canvas>
<input type="button" style="z-index:2; position:absolute; top:x; left:y" value="test"/>

where x and y are numbers. 其中xy是数字。

画布中的HTML是不可能的,但也许你可以绝对定位你的元素,以便它们在画布上“漂浮”,但不在其中。

I don't believe you can 'put' HTML content inside a canvas tag. 我不相信你可以在画布标签中“放置”HTML内容。 Whatever you put in there will actually be displayed if the browser doesn't support <canvas> . 如果浏览器支持<canvas> ,那么无论你放入什么内容都会显示出来。

You can, however, position your buttons absolutely over top of a canvas or render areas in your canvas that 'look' like buttons and handle the events yourself (a lot of work). 但是,您可以将按钮完全放在画布顶部或渲染画布中的“看起来像按钮”并自己处理事件的区域(很多工作)。

One way to add button dynamically on the top of the canvas is following the next two points: 1. Making zIndex of the button higher than canvas 2. Position the button using absolute positioning with desired top and left value 在画布顶部动态添加按钮的一种方法是遵循以下两点:1。使按钮的zIndex高于画布2.使用具有所需顶部和左侧值的绝对定位来定位按钮

Jsfiddle: https://jsfiddle.net/n2EYw/398/ Jsfiddle: https ://jsfiddle.net/n2EYw/398/

HTML: HTML:

<canvas id="canvas" width="200" height="200">           
 </canvas>

CSS: CSS:

canvas {
    border: 1px dotted black;
    background: navy;
}

JavaScript: JavaScript的:

var $testButton = $('<input/>').attr({
    type: 'button',
    name: 'btn1',
    value: 'TestButton',
    id: 'testButton',
    style: 'position:absolute; top:50px;left:100px; zindex:2'
});
$('body').append($testButton);
$(document).on("click", "#testButton", function() {
    alert('button clicked');
});

HTML inside of canvas is not possible. 画布内的HTML是不可能的。
But if you really want to use buttons, why don't you try positioning the buttons on top of the canvas? 但如果您真的想使用按钮,为什么不尝试将按钮放在画布顶部?

You can put a button inside the canvas (png, jpg, svg and text), using the Canvate library. 您可以使用Canvate库在画布(png,jpg,svg和text)中放置一个按钮。 http://www.sakuracode.com/canvate http://www.sakuracode.com/canvate

Here you are a sample of a draging button inside the canvas. 在这里,您可以看到画布内的拖动按钮。

container.startDrag();

https://codepen.io/EiseiKashi/pen/BxNbmj https://codepen.io/EiseiKashi/pen/BxNbmj

You can use my DropdownMenu for put an HTML button or menu inside the canvas. 您可以使用我的DropdownMenu在画布中放置HTML按钮或菜单。

Example of code: 代码示例

<div class="container" id="containerDSE">
    <canvas id="canvas"></canvas>
</div>
<script>
var elContainer = document.getElementById( "containerDSE" ),
elCanvas = elContainer.querySelector( 'canvas' );

dropdownMenu.create( [

    {

        name: 'Button',
        onclick: function ( event ) {

            var message = 'Button onclick';
            //console.log( message );
            alert( message )

        },

    },

], {

    elParent: elContainer,
    canvas: elCanvas,
    decorations: 'Transparent',

} );
</script>

Example of using. 使用示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM