{"id":88,"date":"2025-11-22T09:48:04","date_gmt":"2025-11-22T01:48:04","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=88"},"modified":"2025-11-22T09:48:04","modified_gmt":"2025-11-22T01:48:04","slug":"%e5%9f%ba%e4%ba%8e-html5-canvas-%e5%ae%9e%e7%8e%b0%e5%9b%be%e7%89%87%e6%97%8b%e8%bd%ac%e4%b8%8e%e4%b8%8b%e8%bd%bd%e5%8a%9f%e8%83%bd%e5%ae%8c%e6%95%b4%e4%bb%a3%e7%a0%81%e5%b1%95%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/11\/22\/%e5%9f%ba%e4%ba%8e-html5-canvas-%e5%ae%9e%e7%8e%b0%e5%9b%be%e7%89%87%e6%97%8b%e8%bd%ac%e4%b8%8e%e4%b8%8b%e8%bd%bd%e5%8a%9f%e8%83%bd%e5%ae%8c%e6%95%b4%e4%bb%a3%e7%a0%81%e5%b1%95%e7%a4%ba\/","title":{"rendered":"\u57fa\u4e8e HTML5 Canvas \u5b9e\u73b0\u56fe\u7247\u65cb\u8f6c\u4e0e\u4e0b\u8f7d\u529f\u80fd(\u5b8c\u6574\u4ee3\u7801\u5c55\u793a)"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4e2a <strong>\u57fa\u4e8e HTML5 Canvas \u5b9e\u73b0\u201c\u56fe\u7247\u65cb\u8f6c + \u4e0b\u8f7d\u201d\u201d\u7684\u5b8c\u6574\u53ef\u8fd0\u884c\u793a\u4f8b\u4ee3\u7801<\/strong>\uff0c\u5305\u542b\uff1a<\/p>\n\n\n\n<p>\u2714 \u56fe\u7247\u4e0a\u4f20<br>\u2714 Canvas \u7ed8\u5236\u56fe\u7247<br>\u2714 \u65cb\u8f6c 90\u00b0 \/ 180\u00b0 \/ 270\u00b0<br>\u2714 \u81ea\u7531\u8f93\u5165\u89d2\u5ea6\u65cb\u8f6c<br>\u2714 \u4e0b\u8f7d\u65cb\u8f6c\u540e\u7684\u56fe\u7247<br>\u2714 \u7eaf HTML5 + \u539f\u751f JS\uff0c\u65e0\u9700\u4efb\u4f55\u5e93<\/p>\n\n\n\n<p>\u590d\u5236\u5373\u53ef\u76f4\u63a5\u8fd0\u884c\uff0c\u975e\u5e38\u9002\u5408\u79fb\u52a8\u7aef\u6216 Web \u9879\u76ee\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 <strong>\u5b8c\u6574\u4ee3\u7801\uff08HTML + CSS + JavaScript \u4e00\u4f53\uff09<\/strong><\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;!DOCTYPE html&gt;\n&amp;lt;html lang=&quot;zh-cn&quot;&gt;\n&amp;lt;head&gt;\n&amp;lt;meta charset=&quot;UTF-8&quot;&gt;\n&amp;lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n&amp;lt;title&gt;Canvas \u56fe\u7247\u65cb\u8f6c\u4e0e\u4e0b\u8f7d Demo&amp;lt;\/title&gt;\n\n&amp;lt;style&gt;\n    body {\n        font-family: Arial, sans-serif;\n        padding: 12px;\n        background: #f3f3f3;\n    }\n    .box {\n        background: #fff;\n        padding: 15px;\n        border-radius: 10px;\n        box-shadow: 0 2px 10px rgba(0,0,0,.1);\n    }\n    canvas {\n        max-width: 100%;\n        margin-top: 15px;\n        background: #eee;\n        border-radius: 6px;\n    }\n    button {\n        margin: 5px 5px 5px 0;\n        padding: 8px 16px;\n        border: none;\n        border-radius: 6px;\n        background: #007bff;\n        color: #fff;\n    }\n    input&#x5B;type=&#039;number&#039;] {\n        width: 80px;\n        padding: 6px;\n        border-radius: 6px;\n        border: 1px solid #aaa;\n    }\n&amp;lt;\/style&gt;\n&amp;lt;\/head&gt;\n\n&amp;lt;body&gt;\n\n&amp;lt;h2&gt;Canvas \u56fe\u7247\u65cb\u8f6c\u4e0e\u4e0b\u8f7d\u793a\u4f8b&amp;lt;\/h2&gt;\n\n&amp;lt;div class=&quot;box&quot;&gt;\n    &amp;lt;input type=&quot;file&quot; id=&quot;upload&quot; accept=&quot;image\/*&quot;&gt;\n    &amp;lt;br&gt;\n\n    &amp;lt;button onclick=&quot;rotate(90)&quot;&gt;\u65cb\u8f6c 90\u00b0&amp;lt;\/button&gt;\n    &amp;lt;button onclick=&quot;rotate(180)&quot;&gt;\u65cb\u8f6c 180\u00b0&amp;lt;\/button&gt;\n    &amp;lt;button onclick=&quot;rotate(270)&quot;&gt;\u65cb\u8f6c 270\u00b0&amp;lt;\/button&gt;\n\n    &amp;lt;br&gt;\n    &amp;lt;input id=&quot;angleInput&quot; type=&quot;number&quot; value=&quot;45&quot;&gt;\n    &amp;lt;button onclick=&quot;rotateCustom()&quot;&gt;\u81ea\u5b9a\u4e49\u89d2\u5ea6\u65cb\u8f6c&amp;lt;\/button&gt;\n\n    &amp;lt;br&gt;\n    &amp;lt;button onclick=&quot;downloadImage()&quot;&gt;\u4e0b\u8f7d\u65cb\u8f6c\u540e\u7684\u56fe\u7247&amp;lt;\/button&gt;\n&amp;lt;\/div&gt;\n\n&amp;lt;canvas id=&quot;canvas&quot;&gt;&amp;lt;\/canvas&gt;\n\n&amp;lt;script&gt;\n    const upload = document.getElementById(&quot;upload&quot;);\n    const canvas = document.getElementById(&quot;canvas&quot;);\n    const ctx = canvas.getContext(&quot;2d&quot;);\n\n    let img = new Image();\n    let currentAngle = 0;\n\n    \/\/ \u4e0a\u4f20\u56fe\u7247\n    upload.onchange = function (e) {\n        const file = e.target.files&#x5B;0];\n        if (file) {\n            const reader = new FileReader();\n            reader.onload = function (evt) {\n                img.src = evt.target.result;\n            };\n            reader.readAsDataURL(file);\n        }\n    };\n\n    \/\/ \u56fe\u7247\u52a0\u8f7d\u5b8c\u6210\u540e\u7ed8\u5236\n    img.onload = function () {\n        drawImage();\n    };\n\n    \/\/ \u7ed8\u5236\u56fe\u7247\uff08\u5e26\u65cb\u8f6c\uff09\n    function drawImage() {\n        const angleInRad = currentAngle * Math.PI \/ 180;\n\n        \/\/ \u65cb\u8f6c\u540e\u56fe\u7247\u7684\u5bbd\u9ad8\u91cd\u65b0\u8ba1\u7b97\n        const width = img.width;\n        const height = img.height;\n\n        \/\/ \u65cb\u8f6c 90\u00b0 \/ 270\u00b0 \u65f6\u5bbd\u9ad8\u5bf9\u8c03\n        const cw = Math.abs(Math.cos(angleInRad)) * width + Math.abs(Math.sin(angleInRad)) * height;\n        const ch = Math.abs(Math.sin(angleInRad)) * width + Math.abs(Math.cos(angleInRad)) * height;\n\n        canvas.width = cw;\n        canvas.height = ch;\n\n        ctx.clearRect(0, 0, cw, ch);\n\n        ctx.save();\n        \/\/ \u79fb\u52a8 canvas \u539f\u70b9\u5230\u4e2d\u5fc3\n        ctx.translate(cw \/ 2, ch \/ 2);\n        \/\/ \u65cb\u8f6c\u753b\u5e03\n        ctx.rotate(angleInRad);\n        \/\/ \u7ed8\u5236\u56fe\u7247\uff08\u8ba9\u56fe\u7247\u4e2d\u5fc3\u5bf9\u9f50\uff09\n        ctx.drawImage(img, -width \/ 2, -height \/ 2);\n        ctx.restore();\n    }\n\n    \/\/ \u6309\u94ae\u65cb\u8f6c\n    function rotate(angle) {\n        currentAngle = (currentAngle + angle) % 360;\n        drawImage();\n    }\n\n    \/\/ \u81ea\u5b9a\u4e49\u89d2\u5ea6\u65cb\u8f6c\n    function rotateCustom() {\n        const angle = parseFloat(document.getElementById(&quot;angleInput&quot;).value) || 0;\n        currentAngle = (currentAngle + angle) % 360;\n        drawImage();\n    }\n\n    \/\/ \u4e0b\u8f7d\u56fe\u7247\n    function downloadImage() {\n        const link = document.createElement(&quot;a&quot;);\n        link.download = &quot;rotated_image.png&quot;;\n        link.href = canvas.toDataURL(&quot;image\/png&quot;);\n        link.click();\n    }\n&amp;lt;\/script&gt;\n\n&amp;lt;\/body&gt;\n&amp;lt;\/html&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udccc \u529f\u80fd\u70b9\u8bf4\u660e<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u529f\u80fd<\/th><th>\u5df2\u5b9e\u73b0<\/th><\/tr><\/thead><tbody><tr><td>\u4e0a\u4f20\u56fe\u7247<\/td><td>\u2714<\/td><\/tr><tr><td>Canvas \u7ed8\u56fe<\/td><td>\u2714<\/td><\/tr><tr><td>\u81ea\u7531\u65cb\u8f6c\u56fe\u7247<\/td><td>\u2714<\/td><\/tr><tr><td>\u56fe\u7247\u5bbd\u9ad8\u81ea\u9002\u5e94<\/td><td>\u2714<\/td><\/tr><tr><td>\u6309\u94ae\u65cb\u8f6c 90\/180\/270<\/td><td>\u2714<\/td><\/tr><tr><td>\u81ea\u5b9a\u4e49\u89d2\u5ea6\u65cb\u8f6c<\/td><td>\u2714<\/td><\/tr><tr><td>\u4e0b\u8f7d\u65cb\u8f6c\u540e\u56fe\u7247<\/td><td>\u2714<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53ea\u8981\u544a\u8bc9\u6211\u9700\u8981\u54ea\u4e2a\u7248\u672c\uff0c\u6211\u9a6c\u4e0a\u5199\u7ed9\u4f60\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4e2a \u57fa\u4e8e HTML5 Canvas \u5b9e\u73b0\u201c\u56fe\u7247\u65cb\u8f6c + \u4e0b\u8f7d\u201d\u201d\u7684\u5b8c\u6574&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/11\/22\/%e5%9f%ba%e4%ba%8e-html5-canvas-%e5%ae%9e%e7%8e%b0%e5%9b%be%e7%89%87%e6%97%8b%e8%bd%ac%e4%b8%8e%e4%b8%8b%e8%bd%bd%e5%8a%9f%e8%83%bd%e5%ae%8c%e6%95%b4%e4%bb%a3%e7%a0%81%e5%b1%95%e7%a4%ba\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-html5"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions\/89"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}