Javascript-форум (https://javascript.ru/forum/)
-   Мобильный JavaScript (https://javascript.ru/forum/mobile/)
-   -   Мобильная версия рисовалки (https://javascript.ru/forum/mobile/84110-mobilnaya-versiya-risovalki.html)

wweelltt 04.06.2022 01:22

Мобильная версия рисовалки
 
Здравствуйте, подскажите пожалуйста.:help: Как сделать мобильную версию вот этой рисовалки, чтобы боковые грани канваса доходили до конца экрана телефона и подстраивались под разное разрешение экранов?

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>www</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
    <canvas id="jsCanvas" class="canvas"></canvas>  
  </div>
    <div class="controls">
      <div class="controls__range">
        <input type="range" id="jsRange" min="1.0" max="16.0" value="8.0" step="1.0">
      </div>
      <div class="controls__btns">
        <button id="jsMode">Рисовать</button>
        <button id="jsClear">Очистить</button>
        <button id="jsSave">Сохранить</button>
      </div>
        <div class="controls__colors" id="jsColors">
            <div class="controls__color jsColor" style="background-color:red"></div>
            <div class="controls__color jsColor" style="background-color:orangered"></div>
            <div class="controls__color jsColor" style="background-color:yellow"></div>
            <div class="controls__color jsColor" style="background-color:green"></div>
            <div class="controls__color jsColor" style="background-color:rgb(0, 162, 232)"></div>
            <div class="controls__color jsColor" style="background-color:blue"></div>
            <div class="controls__color jsColor" style="background-color:purple"></div>
            <div class="controls__color jsColor" style="background-color:#000000"></div>
        </div>
    </div>
  <script src="app.js"></script>
</body>
</html>


JS
const btn = document.getElementById('jsClear');
const mode = document.getElementById('jsMode');
const canvas = document.getElementById('jsCanvas');
const colors = document.getElementsByClassName ('jsColor');
const ctx = canvas.getContext('2d');
const range = document.getElementById('jsRange');
const saveBtn = document.getElementById('jsSave');

let img = new Image();
img.src = "https://catherineasquithgallery.com/uploads/posts/2021-03/1614612398_1-p-fon-neba-dlya-fotoshopa-1.jpg";
window.addEventListener('load', function() {
  ctx.drawImage(img, 0, 0, 540, 675);
});

let painting = false;

canvas.height=675;
canvas.width=540;
 
ctx.lineWidth = 8;
ctx.lineCap = "round";
ctx.strokeStyle = '#0c0c0c';

function onClear() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.drawImage(img, 0, 0, 540, 675);
}

function handleColorClick(event){
  const color = event.target.style.backgroundColor;
  ctx.strokeStyle = color;
}

function handleRangeChange(event){
  const rangeValue = event.target.value;
  ctx.lineWidth = rangeValue;
}

function handleModeClick(){
  if (painting === true){
      painting = false;
      mode.innerText = 'Рисовать';
  } else {
    painting = true;
      mode.innerText = 'Штамп';
  }
  }

var image = new Image;
image.src = 'https://www.pinclipart.com/picdir/big/285-2856763_tangled-sun-png-clipart-transparent-png.png';

function drawIfPressed(e) {
console.log(e.type)
var x = e.offsetX;
var y = e.offsetY;
var dx = e.movementX;
var dy = e.movementY;
  if (e.type == 'click' && !painting) {
    var width = 40;
ctx.drawImage(image, x - width / 2, y - width / 2, width, width);
 } else if (e.buttons > 0 && painting) {
    ctx.beginPath();
    ctx.moveTo(x, y);
    ctx.lineTo(x - dx, y - dy);
    ctx.stroke();
    ctx.closePath();
}
}

function handleCM(event){
  event.preventDefault();
}

function handleSaveClick(){
  const Image = canvas.toDataURL('image/png');
  const link = document.createElement('a');
  link.href = Image;
  link.download = "download.png";
  link.click();
}
0
canvas.addEventListener('mousemove', drawIfPressed);
canvas.addEventListener('click', drawIfPressed);
canvas.addEventListener('contextmenu', handleCM);
btn.addEventListener('click', onClear);

Array.from(colors).forEach(color => color.addEventListener('click', handleColorClick));

if (range){
  range.addEventListener('input', handleRangeChange);
}

if (mode) {
  mode.addEventListener('click', handleModeClick);
}

if (saveBtn){
  saveBtn.addEventListener('click', handleSaveClick);
}


CSS
body {
    background-color: rgb(172, 172, 172);
    font-family: -apple-system, BlinkMacSystemFont, Verdana, Arial, Helvetica, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 50px;
}

.canvas {
    width: 540px;
    height: 675px;
    background-color: white;
    border: 2px solid black;
}

.controls{
    margin-top: 40px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.controls .controls__btns {
    margin-bottom: 30px;
}

.controls__btns button {
    cursor: pointer;
    background-color: white;
    text-align: center;
    font-weight: 600;
    font-size: 16px;
}

.controls .controls__colors {
    display: flex;
}

.controls__colors .controls__color {
    width: 50px;
    height: 50px;
    cursor: pointer;
    margin: 2px;
    border: 2px solid black;
    float: left;
}

.controls .controls__range {
    margin-bottom: 30px;
}


Часовой пояс GMT +3, время: 15:05.