怎么打出div

时间:2013-02-18 10:05:05

标签: html css html5 canvas

我有一个包含文本的div元素,我想要消灭整个元素,以便两条线交叉作为符号X.

text-decoration:line-through

Strikeout文字不合适。

我想要一个45度的线。我希望借助画布可以实现这一点。

1 个答案:

答案 0 :(得分:1)

HTML:

<div id="con" style="position:relative;">
  <canvas id="canvas" style="position:absolute; top:0; left:0; z-index:0;">
  </canvas>
  <div id="text">
  .....
  </div>
</div>

JS:

  var width = document.getElementById('text').offsetWidth;
  var height = document.getElementById('text').offsetHeight;
  var canvas = document.getElementById('canvas');
  canvas.width = width;
  canvas.height = height;
  var context = canvas.getContext('2d');
  context.beginPath();
  context.moveTo(0, 0);
  context.lineTo(width, height);
  context.moveTo(width, 0);
  context.lineTo(0, height);
  context.stroke();