• 5个前端练手项目(html css js canvas)


    前言:

    首先祝大家端午节快乐。本篇文章有5个练手项目
    对于刚学完前端三剑客的你们。应该是一个很好的实践

    目录

    ??.跑马灯

    1.1效果图:

    1.2思路解析

    1.3源码

    ??.彩虹爱心

    2.1效果图

    2.2思路解析

    2.3源码

    ??.闹钟

    3.1效果图

    3.2思路解析

    3.3源码

    ??.自制笔记本

    4.1效果展示

    4.2思路解析

    4.3源码

    ??.自定义写字台(也可自定义字的样式)

    5.1效果展示

    ?5.2思路解析

    5.3源码



    1.跑马灯

    1.1效果图:


    1.2思路解析

    在这个项目中,在html中创立20个span标签

    每个span标签设置style为–i:数字的样式用于

    在css中动态分配圆圈分几份,transform: rotate(calc(18deg*var(–i)))

    利用filter属性结合关键帧动态切换颜色。同时设置每一个span标签进行

    旋转


    1.3源码

    
    
    
    

    loading

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104

    2.彩虹爱心


    2.1效果图


    2.2思路解析

    搭建基本的html结构,采用的svg技术

    通过js动态改变颜色,以及动态实现切换图形


    2.3源码

    
      
    
        
    
        
    
        
    
    
    
    
    
    
    
    const colors = ["#e03776","#8f3e98","#4687bf","#3bab6f","#f9c25e","#f47274"];
    const SVG_NS = 'http://www.w3.org/2000/svg';
    const SVG_XLINK = "http://www.w3.org/1999/xlink";
    
    let heartsRy = []
    
    function useTheHeart(n){
      let use = document.createElementNS(SVG_NS, 'use');
      use.n = n;
      use.setAttributeNS(SVG_XLINK, 'xlink:href', '#heart');
      use.setAttributeNS(null, 'transform', `scale(${use.n})`);
      use.setAttributeNS(null, 'fill', colors[n%colors.length]);
      use.setAttributeNS(null, 'x', -69);
      use.setAttributeNS(null, 'y', -69);
      use.setAttributeNS(null, 'width', 138);
      use.setAttributeNS(null, 'height', 138);
      
      heartsRy.push(use)
      hearts.appendChild(use);
    }
    
    for(let n = 18; n >= 0; n--){useTheHeart(n)}
    
    function Frame(){
      window.requestAnimationFrame(Frame);
      for(let i = 0; i < heartsRy.length; i++){
        if(heartsRy[i].n < 18){heartsRy[i].n +=.01
         }else{
         heartsRy[i].n = 0;
         hearts.appendChild(heartsRy[i])
        }
        heartsRy[i].setAttributeNS(null, 'transform', `scale(${heartsRy[i].n})`);
      }
    }
    
    Frame()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    3.闹钟


    3.1效果图


    3.2思路解析

    搭建基本的html结构,动态得到实时的时,分,秒

    通过Date()函数获得。将得到的数字根据逻辑,绑定

    给各div结构,实行动态旋转。点击按钮,改变背景颜色


    3.3源码

    html:

    
      
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    css:

    @import url('https://fonts.googleapis.com/css?family=Heebo:300&display=swap');
    
    * {
      box-sizing: border-box;
    }
    
    :root {
      --primary-color: #000;
      --secondary-color: #fff;
    }
    
    html {
      transition: all 0.5s ease-in;
    }
    
    html.dark {
      --primary-color: #fff;
      --secondary-color: #333;
    }
    
    html.dark {
      background-color: #111;
      color: var(--primary-color);
    }
    
    body {
      font-family: 'Heebo', sans-serif;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100vh;
      overflow: hidden;
      margin: 0;
    }
    
    .toggle {
      cursor: pointer;
      background-color: var(--primary-color);
      color: var(--secondary-color);
      border: 0;
      border-radius: 4px;
      padding: 8px 12px;
      position: absolute;
      top: 100px;
    }
    
    .toggle:focus {
      outline: none;
    }
    
    .clock-container {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: center;
    }
    
    .clock {
      position: relative;
      width: 200px;
      height: 200px;
    }
    
    .needle {
      background-color: var(--primary-color);
      position: absolute;
      top: 50%;
      left: 50%;
      height: 65px;
      width: 3px;
      transform-origin: bottom center;
      transition: all 0.5s ease-in;
    }
    
    .needle.hour {
      transform: translate(-50%, -100%) rotate(0deg);
    }
    
    .needle.minute {
      transform: translate(-50%, -100%) rotate(0deg);
      height: 100px;
    }
    
    .needle.second {
      transform: translate(-50%, -100%) rotate(0deg);
      height: 100px;
      background-color: #e74c3c;
    }
    
    .center-point {
      background-color: #e74c3c;
      width: 10px;
      height: 10px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      border-radius: 50%;
    }
    
    .center-point::after {
      content: '';
      background-color: var(--primary-color);
      width: 5px;
      height: 5px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      border-radius: 50%;
    }
    
    .time {
      font-size: 60px;
    }
    
    .date {
      color: #aaa;
      font-size: 14px;
      letter-spacing: 0.3px;
      text-transform: uppercase;
    }
    
    .date .circle {
      background-color: var(--primary-color);
      color: var(--secondary-color);
      border-radius: 50%;
      height: 18px;
      width: 18px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      line-height: 18px;
      transition: all 0.5s ease-in;
      font-size: 12px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136

    js:

    const hourEl = document.querySelector('.hour')
    const minuteEl = document.querySelector('.minute')
    const secondEl = document.querySelector('.second')
    const timeEl = document.querySelector('.time')
    const dateEl = document.querySelector('.date')
    const toggle = document.querySelector('.toggle')
    
    const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    
    toggle.addEventListener('click', (e) => {
        const html = document.querySelector('html')
        if (html.classList.contains('dark')) {
            html.classList.remove('dark')
            e.target.innerHTML = 'Dark mode'
        } else {
            html.classList.add('dark')
            e.target.innerHTML = 'Light mode'
        }
    })
    
    function setTime() {
        const time = new Date();
        const month = time.getMonth()
        const day = time.getDay()
        const date = time.getDate()
        const hours = time.getHours()
        const hoursForClock = hours >= 13 ? hours % 12 : hours;
        const minutes = time.getMinutes()
        const seconds = time.getSeconds()
        const ampm = hours >= 12 ? 'PM' : 'AM'
    
        hourEl.style.transform = `translate(-50%, -100%) rotate(${scale(hoursForClock, 0, 12, 0, 360)}deg)`
        minuteEl.style.transform = `translate(-50%, -100%) rotate(${scale(minutes, 0, 60, 0, 360)}deg)`
        secondEl.style.transform = `translate(-50%, -100%) rotate(${scale(seconds, 0, 60, 0, 360)}deg)`
    
        timeEl.innerHTML = `${hoursForClock}:${minutes < 10 ? `0${minutes}` : minutes} ${ampm}`
        dateEl.innerHTML = `${days[day]}, ${months[month]} ${date}`
    }
    
    // StackOverflow https://stackoverflow.com/questions/10756313/javascript-jquery-map-a-range-of-numbers-to-another-range-of-numbers
    const scale = (num, in_min, in_max, out_min, out_max) => {
        return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
    }
    
    setTime()
    
    setInterval(setTime, 1000)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    4.自制笔记本


    4.1效果展示


    4.2思路解析

    通过js实现动态添加DOM结构,绑定创建出DOM结构的

    添加,删除按钮。实现监听事件。实现动态改变DOM结构

    其他的就是设置css的相关属性,


    4.3源码

    html:

    
      
      
        
        
    
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    css:

    @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;400&display=swap');
    
    * {
      box-sizing: border-box;
      outline: none;
    }
    
    body {
      background-color: #7bdaf3;
      font-family: 'Poppins', sans-serif;
      display: flex;
      flex-wrap: wrap;
      margin: 0;
      padding-top: 3rem;
    }
    
    .add {
      position: fixed;
      top: 1rem;
      right: 1rem;
      background-color: #9ec862;
      color: #fff;
      border: none;
      border-radius: 3px;
      padding: 0.5rem 1rem;
      cursor: pointer;
    }
    
    .add:active {
      transform: scale(0.98);
    }
    
    .note {
      background-color: #fff;
      box-shadow: 0 0 10px 4px rgba(0, 0, 0, 0.1);
      margin: 30px 20px;
      height: 400px;
      width: 400px;
      overflow-y: scroll;
    }
    
    .note .tools {
      background-color: #9ec862;
      display: flex;
      justify-content: flex-end;
      padding: 0.5rem;
    }
    
    .note .tools button {
      background-color: transparent;
      border: none;
      color: #fff;
      cursor: pointer;
      font-size: 1rem;
      margin-left: 0.5rem;
    }
    
    .note textarea {
      outline: none;
      font-family: inherit;
      font-size: 1.2rem;
      border: none;
      height: 400px;
      width: 100%;
      padding: 20px;
    }
    
    .main {
      padding: 20px;
    }
    
    .hidden {
      display: none;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    js:

    const addBtn = document.getElementById('add')
    
    const notes = JSON.parse(localStorage.getItem('notes'))
    
    if(notes) {
        notes.forEach(note => addNewNote(note))
    }
    
    addBtn.addEventListener('click', () => addNewNote())
    
    function addNewNote(text = '') {
        const note = document.createElement('div')
        note.classList.add('note')
    
        note.innerHTML = `
        
    ` const editBtn = note.querySelector('.edit') const deleteBtn = note.querySelector('.delete') const main = note.querySelector('.main') const textArea = note.querySelector('textarea') textArea.value = text main.innerHTML = marked(text) deleteBtn.addEventListener('click', () => { note.remove() updateLS() }) editBtn.addEventListener('click', () => { main.classList.toggle('hidden') textArea.classList.toggle('hidden') }) textArea.addEventListener('input', (e) => { const { value } = e.target main.innerHTML = marked(value) updateLS() }) document.body.appendChild(note) } function updateLS() { const notesText = document.querySelectorAll('textarea') const notes = [] notesText.forEach(note => notes.push(note.value)) localStorage.setItem('notes', JSON.stringify(notes)) }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    5.自定义写字台(也可自定义字的样式)


    5.1效果展示


    5.2思路解析

    搭建html结构,创建canvas标签

    绑定设置的结构比如+,-,颜色改变

    动态设置并获取他的值,然后将这些值动态的

    设置为canvas语法中设置渲染的宽度,以及设置

    颜色的属性


    5.3源码

    html:

    
     
    10
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    css:

    @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
    
    * {
      box-sizing: border-box;
    }
    
    body {
      background-color: #f5f5f5;
      font-family: 'Roboto', sans-serif;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      height: 100vh;
      margin: 0;
    }
    
    canvas {
      border: 2px solid steelblue;
    }
    
    .toolbox {
      background-color: steelblue;
      border: 1px solid slateblue;
      display: flex;
      width: 804px;
      padding: 1rem;
    }
    
    .toolbox > * {
      background-color: #fff;
      border: none;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      font-size: 2rem;
      height: 50px;
      width: 50px;
      margin: 0.25rem;
      padding: 0.25rem;
      cursor: pointer;
    }
    
    .toolbox > *:last-child {
      margin-left: auto;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    js:

    const canvas = document.getElementById('canvas');
    const increaseBtn = document.getElementById('increase');
    const decreaseBtn = document.getElementById('decrease');
    const sizeEL = document.getElementById('size');
    const colorEl = document.getElementById('color');
    const clearEl = document.getElementById('clear');
    
    const ctx = canvas.getContext('2d');
    
    let size = 10
    let isPressed = false
    colorEl.value = 'black'
    let color = colorEl.value
    let x
    let y
    
    canvas.addEventListener('mousedown', (e) => {
        isPressed = true
    
        x = e.offsetX
        y = e.offsetY
    })
    
    document.addEventListener('mouseup', (e) => {
        isPressed = false
    
        x = undefined
        y = undefined
    })
    
    canvas.addEventListener('mousemove', (e) => {
        if(isPressed) {
            const x2 = e.offsetX
            const y2 = e.offsetY
    
            drawCircle(x2, y2)
            drawLine(x, y, x2, y2)
    
            x = x2
            y = y2
        }
    })
    
    function drawCircle(x, y) {
        ctx.beginPath();
        ctx.arc(x, y, size, 0, Math.PI * 2)
        ctx.fillStyle = color
        ctx.fill()
    }
    
    function drawLine(x1, y1, x2, y2) {
        ctx.beginPath()
        ctx.moveTo(x1, y1)
        ctx.lineTo(x2, y2)
        ctx.strokeStyle = color
        ctx.lineWidth = size * 2
        ctx.stroke()
    }
    
    function updateSizeOnScreen() {
        sizeEL.innerText = size
    }
    
    increaseBtn.addEventListener('click', () => {
        size += 5
    
        if(size > 50) {
            size = 50
        }
    
        updateSizeOnScreen()
    })
    
    decreaseBtn.addEventListener('click', () => {
        size -= 5
    
        if(size < 5) {
            size = 5
        }
    
        updateSizeOnScreen()
    })
    
    colorEl.addEventListener('change', (e) => color = e.target.value)
    
    clearEl.addEventListener('click', () => ctx.clearRect(0,0, canvas.width, canvas.height))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    在最后,如果觉得博主写的还行,期待??点赞 ??评论 ??收藏

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    LightDB数据库中的模式
    027、工具_redis-benchmark
    hiveSql 相互关注问题
    解析小结—自用
    在亚马逊云科技控制台上创建 Amazon Cognito 用户池
    使用HBuilderX将vue或H5项目打包app
    奥斯卡·王尔德
    CSS 常用样式 之字体属性
    【毕业设计】基于STM32的天气预报盒子 - 嵌入式 单片机 物联网
    一文总结现代 C++ 中的初始化
  • 原文地址:https://blog.csdn.net/drnrrwfs/article/details/126098070