且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止文本选择外部的HTML5画布上双击?

更新时间:2023-12-02 11:05:16

首先让我注意,你的画布不填充页面的宽度,它只有100像素宽。宽度和高度的画布属性总是解析为像素,因此对于Canvas标记,写入width =100%就意味着100像素。

Firstly let me note that your canvas is not filling the width of the page, it is only 100 pixels wide. Width and height canvas attributes always parse to pixels, so writing width="100%" just means 100 pixels as far as the Canvas tag is concerned.

,写在javasript:

To answer your question, write in javasript:

//give your canvas an id, I used 'can'    
var canvas = document.getElementById('can');
canvas.onselectstart = function () { return false; }

双击文本问题将不再出现。

The double-click text problem will no longer occur.