画布线的质量差以及画布坐标与html的比较

时间:2017-11-09 12:49:21

标签: javascript html5 canvas

1)我正在练习画布线。所以我改变了canvas标签的大小,然后绘制线条。但这条线很模糊。它为什么会发生,如何改变呢?

2)我还有第二个问题:是否可以在canvas标签内连接线的坐标和标签li?例如:当我想点击li项时,二次曲线的末端应该直接移动到li项。

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.lineWidth = 100;
ctx.lineWidth = 1; // толщина линии
ctx.moveTo(11, 100); //передвигаем перо
ctx.lineTo(350, 100); //рисуем линию
ctx.fillStyle = "purple";
ctx.strokeStyle = "purple";
ctx.stroke();
ctx.lineWidth=100;
ctx.lineWidth= 1;
ctx.moveTo(11,100);
ctx.quadraticCurveTo(26, 0, 50, 100);
ctx.stroke();
* {
    margin: 0;
    padding: 0;
}

.ruler {
    display: flex;
    flex-direction: column;
    background: url("sprite.webp") no-repeat;
    padding-bottom: 67px;
    width: 877px;
    margin: 0 auto;
}

.ruler-container {
    white-space: nowrap;
    margin: 0 auto;
    max-width: 960px;
    padding-top: 270px;
}

.ruler-list li {
    list-style-type: none;
    display: flex;
}

.ruler-list__item {
    font-size: 22px;
    margin-right: 35px;
    margin-left: -7px;
}

.double__numbers {
    margin-right: 24px;
}

.bold {
    /*margin-top: -4px;*/
    font-weight: bold;
}

.ruler-list {
    display: flex;
    flex-direction: row;
    margin-top: 19px;
    margin-left: 30px;
    padding: 0;
}

.ruler-axis {
    width: 813px;
    height: 2px;
    background-color: #000000;
    margin-left: 33px;
    margin-top: 20px;
    position: relative;
}

.ruler-axis__item-block {
    position: relative;
    width: 11px;
    height: 1px;
    transform: rotate(90deg);
    background-color: #000000;
    top: -12px;
    margin-right: 31px;
    margin-left: -7px;
    font-size: 20px;
    display: inline-block;
}

.ruler-axis::before {
    content: " ";
    position: absolute;
    width: 16px;
    height: 2px;
    right: -4px;
    top: 6px;
    transform: rotate(-46deg);
    background-color: #000000;
}

.ruler-axis::after {
    content: " ";
    position: absolute;
    width: 16px;
    height: 2px;
    right: -4px;
    top: -6px;
    transform: rotate(46deg);
    background-color: #000000;
}

.double {
    margin-right: 34px;
}

input[type='number'] {
    -moz-appearance: textfield;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

.user-input {
    display: flex;
    width: 500px;
    margin: 0 auto;
    margin-bottom: 95px;
}

input[type='number'] {
    padding-bottom: 0;
    text-align: center;
    padding-top: 0;
    padding-left: 0;
    width: 76px;
    font-size: 66px;
}

span {
    display: block;
}

#canvas {
    position: absolute;
    width: 818px;
    height: 132px;
    margin-top: -68px;
}

.user-input span {
    font-size: 75px;
    margin-left: 20px;
}

.input__container {
    margin-left: 21px;
    margin-top: 0;
}

.hidden {
    display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/postscribe/2.0.8/postscribe.min.js"></script>
<main class="main-class">
  <div class="ruler-container">
    <div class="user-input">
      <span class="user-input__number">7</span>
      <span class="user-input__sign">+</span>
      <span class="user-input__number">8</span>
      <span class="user-inupt__total">=</span>
      <span class="user-inupt__mockup">?</span>
      <div class="input__container hidden">
        <label for="result" class="input-container__label">
           <input type="number" max="500" class="input-container__input-block" id="result">
        </label>
      </div>
    </div>
    <div class="ruler">
      <div class="ruler-axis">
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
      </div>
      <canvas id="canvas">
                Your browser does not support the HTML5 canvas tag.</canvas>
      <ul class="ruler-list">
        <li class="ruler-list__item bold">0</li>
        <li class="ruler-list__item">1</li>
        <li class="ruler-list__item">2</li>
        <li class="ruler-list__item">3</li>
        <li class="ruler-list__item">4</li>
        <li class="ruler-list__item bold">5</li>
        <li class="ruler-list__item">6</li>
        <li class="ruler-list__item">7</li>
        <li class="ruler-list__item">8</li>
        <li class="ruler-list__item">9</li>
        <li class="ruler-list__item bold double__numbers">10</li>
        <li class="ruler-list__item double__numbers">11</li>
        <li class="ruler-list__item double__numbers">12</li>
        <li class="ruler-list__item double__numbers">13</li>
        <li class="ruler-list__item double__numbers">14</li>
        <li class="ruler-list__item double__numbers">15</li>
        <li class="ruler-list__item double__numbers">16</li>
        <li class="ruler-list__item double__numbers">17</li>
        <li class="ruler-list__item double__numbers">18</li>
        <li class="ruler-list__item double__numbers">19</li>
        <li class="ruler-list__item bold double__numbers">20</li>
      </ul>
    </div>
  </div>
</main>

2 个答案:

答案 0 :(得分:1)

您指定了绘制完成后应用的样式的宽度和高度,从而导致缩放而不是调整大小。

&#13;
&#13;
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.strokeStyle = "purple";
ctx.lineWidth= 1;
ctx.moveTo(32,90);
ctx.quadraticCurveTo(96, 0, 160, 90);
ctx.stroke();
&#13;
* {
    margin: 0;
    padding: 0;
}

.ruler {
    display: flex;
    flex-direction: column;
    background: url("sprite.webp") no-repeat;
    padding-bottom: 67px;
    width: 877px;
    margin: 0 auto;
}

.ruler-container {
    white-space: nowrap;
    margin: 0 auto;
    max-width: 960px;
    padding-top: 270px;
}

.ruler-list li {
    list-style-type: none;
    display: flex;
}

.ruler-list__item {
    font-size: 22px;
    margin-right: 35px;
    margin-left: -7px;
}

.double__numbers {
    margin-right: 24px;
}

.bold {
    /*margin-top: -4px;*/
    font-weight: bold;
}

.ruler-list {
    display: flex;
    flex-direction: row;
    margin-top: 19px;
    margin-left: 30px;
    padding: 0;
}

.ruler-axis {
    width: 813px;
    height: 2px;
    background-color: #000000;
    margin-left: 33px;
    margin-top: 20px;
    position: relative;
}

.ruler-axis__item-block {
    position: relative;
    width: 11px;
    height: 1px;
    transform: rotate(90deg);
    background-color: #000000;
    top: -12px;
    margin-right: 31px;
    margin-left: -7px;
    font-size: 20px;
    display: inline-block;
}

.ruler-axis::before {
    content: " ";
    position: absolute;
    width: 16px;
    height: 2px;
    right: -4px;
    top: 6px;
    transform: rotate(-46deg);
    background-color: #000000;
}

.ruler-axis::after {
    content: " ";
    position: absolute;
    width: 16px;
    height: 2px;
    right: -4px;
    top: -6px;
    transform: rotate(46deg);
    background-color: #000000;
}

.double {
    margin-right: 34px;
}

input[type='number'] {
    -moz-appearance: textfield;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

.user-input {
    display: flex;
    width: 500px;
    margin: 0 auto;
    margin-bottom: 95px;
}

input[type='number'] {
    padding-bottom: 0;
    text-align: center;
    padding-top: 0;
    padding-left: 0;
    width: 76px;
    font-size: 66px;
}

span {
    display: block;
}

#canvas {
    position: absolute;
    margin-top: -68px;
}

.user-input span {
    font-size: 75px;
    margin-left: 20px;
}

.input__container {
    margin-left: 21px;
    margin-top: 0;
}

.hidden {
    display: none;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/postscribe/2.0.8/postscribe.min.js"></script>
<main class="main-class">
  <div class="ruler-container">
    <div class="user-input">
      <span class="user-input__number">7</span>
      <span class="user-input__sign">+</span>
      <span class="user-input__number">8</span>
      <span class="user-inupt__total">=</span>
      <span class="user-inupt__mockup">?</span>
      <div class="input__container hidden">
        <label for="result" class="input-container__label">
           <input type="number" max="500" class="input-container__input-block" id="result">
        </label>
      </div>
    </div>
    <div class="ruler">
      <div class="ruler-axis">
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
        <div class="ruler-axis__item-block"></div>
      </div>
      <canvas width=818 height=132 id="canvas">
                Your browser does not support the HTML5 canvas tag.</canvas>
      <ul class="ruler-list">
        <li class="ruler-list__item bold">0</li>
        <li class="ruler-list__item">1</li>
        <li class="ruler-list__item">2</li>
        <li class="ruler-list__item">3</li>
        <li class="ruler-list__item">4</li>
        <li class="ruler-list__item bold">5</li>
        <li class="ruler-list__item">6</li>
        <li class="ruler-list__item">7</li>
        <li class="ruler-list__item">8</li>
        <li class="ruler-list__item">9</li>
        <li class="ruler-list__item bold double__numbers">10</li>
        <li class="ruler-list__item double__numbers">11</li>
        <li class="ruler-list__item double__numbers">12</li>
        <li class="ruler-list__item double__numbers">13</li>
        <li class="ruler-list__item double__numbers">14</li>
        <li class="ruler-list__item double__numbers">15</li>
        <li class="ruler-list__item double__numbers">16</li>
        <li class="ruler-list__item double__numbers">17</li>
        <li class="ruler-list__item double__numbers">18</li>
        <li class="ruler-list__item double__numbers">19</li>
        <li class="ruler-list__item bold double__numbers">20</li>
      </ul>
    </div>
  </div>
</main>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

基于建议的答案。

如果你想要响应图形,你必须使用带有返回值的函数,例如:

 ctx.moveTo(11, 100); //передвигаем перо

让它看起来像这样:

  ctx.moveTo( setWidthInPer( 50 ) , setHeightInPer( 50 ) ); 

或更好地使用变量direct:

  var DOTX1 = 50 , DOTY1 = 50;
  ctx.moveTo( setWidthInPer( DOTX1 ) , setHeightInPer( DOTY1 ) ); 

// **Now you can update position with simple DOTX1 = 25 ....in any time** .

帮助功能:

  function setWidthInPer (per){
   // per = 1 will return  1% of window width always 
   return window.innerWidth / 100 * per;

  }

  function setHeightInPer (per){

   return window.innerHeight / 100 * per;

   }

如果您使用此过程,则不需要对window.onresize执行特殊操作。

此外,您可以将所有绘制代码​​放入自调用函数,animframerequest或setInteval(如果您想更改渲染速度)。

示例:

function draw() {


    ctx.lineWidth = 100;
    ctx.lineWidth = 1; // толщина линии
    ctx.moveTo(11, 100); //передвигаем перо
    ctx.lineTo(350, 100); //рисуем линию
    ctx.fillStyle = "purple";
    ctx.strokeStyle = "purple";
    ctx.stroke();
    ctx.lineWidth=100;
    ctx.lineWidth= 1;
    ctx.moveTo(11,100);
    ctx.quadraticCurveTo(26, 0, 50, 100);
    ctx.stroke();
    window.requestAnimationFrame(draw);

}

window.requestAnimationFrame(draw); 

重要的是,如果你已经使用canvas在画布中全部使用,则不需要li或任何经典的html标签(可能不好)。

用画布画出标尺。