如何在页面加载时同时显示带有空白框或已定义字母预览的两个 div?

huangapple go评论67阅读模式
英文:

How can I show both div with an empty box or preview of the defined alphabet in the divs on page load?

问题

我正在通过定义的字母表来转换用户输入的字母。它有两个div id来显示结果,#outputText 和 #imageContainer。#outputText 显示转换后的文本,#imageContainer 显示转换后的文本图像。这两个id在页面加载时都不会显示结果,而是在用户输入后显示。但我想在页面加载时显示这两个div,其中包含空白框或定义的字母的预览。以下是我尝试使用此代码实现的功能。

<h1>Cool Font Generator</h1>
<input type="text" id="inputText" placeholder="Enter text">
<div id="outputText"></div>
<div class="option">
    <label for="fontSize">Font Size:</label>
    <input type="number" id="fontSize" min="10" max="100" value="48">
</div>
<div class="option">
    <label for="fontColor">Font Color:</label>
    <input type="color" id="fontColor" value="#FFFFFF">
</div>
<div class="option">
    <label for="backgroundColor">Background Color:</label>
    <input type="color" id="backgroundColor" value="#000000">
</div>
<div class="option">
    <label for="fontWeight">Font Weight:</label>
    <select id="fontWeight">
        <option value="normal">Normal</option>
        <option value="bold">Bold</option>
    </select>
</div>

<div id="imageContainer"></div>

<button class="button" id="downloadButton">Download Image</button>
<button class="button" id="copyButton">Copy Text</button>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

希望这对你有所帮助。如果需要更多帮助,请随时提问。

英文:

I am transforming the alphabet on user input by the defined alphabet. It has two div ids to show results #outputText and #imageContainer. #outputText shows transformed text and #imageContainer shows converted text image. Both ids do not show on the page load. it shows results on user input. But I want to show both div with an empty box or preview of the defined alphabet in the divs on page load. This is the code, I tried to achieve by this code.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  const inputText = $(&#39;#inputText&#39;);
  const outputText = $(&#39;#outputText&#39;);
  const imageContainer = $(&#39;#imageContainer&#39;);
  const fontSizeInput = $(&#39;#fontSize&#39;);
  const fontColorInput = $(&#39;#fontColor&#39;);
  const backgroundColorInput = $(&#39;#backgroundColor&#39;);
  const fontWeightSelect = $(&#39;#fontWeight&#39;);
  const downloadButton = $(&#39;#downloadButton&#39;);
  const copyButton = $(&#39;#copyButton&#39;);
  inputText.on(&#39;input&#39;, generateFont);
  fontSizeInput.on(&#39;input&#39;, generateFont);
  fontColorInput.on(&#39;input&#39;, generateFont);
  backgroundColorInput.on(&#39;input&#39;, generateFont);
  fontWeightSelect.on(&#39;change&#39;, generateFont);
  downloadButton.on(&#39;click&#39;, downloadImage);
  copyButton.on(&#39;click&#39;, copyText);

  function generateFont() {
    const text = inputText.val();
    const transformedText = transformText(text);
    outputText.text(transformedText);

    generateImage(transformedText);
  }

  function transformText(text) {
    const alphabet = {
      &#39;A&#39;: &#39;Λ&#39;,
      &#39;B&#39;: &#39;&#223;&#39;,
      &#39;C&#39;: &#39;Ͼ&#39;,
      &#39;D&#39;: &#39;&#208;&#39;,
      &#39;E&#39;: &#39;Σ&#39;,
      &#39;F&#39;: &#39;Ŧ&#39;,
      &#39;G&#39;: &#39;₲&#39;,
      &#39;H&#39;: &#39;Ή&#39;,
      &#39;I&#39;: &#39;ł&#39;,
      &#39;J&#39;: &#39;J&#39;,
      &#39;K&#39;: &#39;Ҝ&#39;,
      &#39;L&#39;: &#39;Ł&#39;,
      &#39;M&#39;: &#39;M&#39;,
      &#39;N&#39;: &#39;И&#39;,
      &#39;O&#39;: &#39;&#216;&#39;,
      &#39;P&#39;: &#39;₱&#39;,
      &#39;Q&#39;: &#39;Q&#39;,
      &#39;R&#39;: &#39;S&#39;,
      &#39;S&#39;: &#39;Ş&#39;,
      &#39;T&#39;: &#39;Ŧ&#39;,
      &#39;U&#39;: &#39;Ц&#39;,
      &#39;V&#39;: &#39;V&#39;,
      &#39;W&#39;: &#39;₩&#39;,
      &#39;X&#39;: &#39;Ж&#39;,
      &#39;Y&#39;: &#39;Ψ&#39;,
      &#39;Z&#39;: &#39;Z&#39;
    };

    const transformedText = text
      .toUpperCase()
      .split(&#39;&#39;)
      .map((char) =&gt; (alphabet.hasOwnProperty(char) ? alphabet[char] : char))
      .join(&#39;&#39;);

    return transformedText;
  }

  function generateImage(text) {
    const canvas = $(&#39;&lt;canvas&gt;&#39;)[0];
    const context = canvas.getContext(&#39;2d&#39;);
    const fontSize = parseInt(fontSizeInput.val());
    const fontColor = fontColorInput.val();
    const backgroundColor = backgroundColorInput.val();
    const fontWeight = fontWeightSelect.val();

    context.font = `${fontWeight} ${fontSize}px &quot;Courier New&quot;, Courier, monospace`;
    const textWidth = context.measureText(text).width;
    const textHeight = fontSize;
    canvas.width = textWidth;
    canvas.height = textHeight;

    context.fillStyle = backgroundColor;
    context.fillRect(0, 0, canvas.width, canvas.height);

    context.fillStyle = fontColor;
    context.font = `${fontWeight} ${fontSize}px &quot;Courier New&quot;, Courier, monospace`;
    context.textBaseline = &#39;middle&#39;;
    context.fillText(text, 0, canvas.height / 2);

    const imageElement = $(&#39;&lt;img&gt;&#39;).attr(&#39;src&#39;, canvas.toDataURL(&#39;image/png&#39;));
    imageContainer.html(imageElement);
  }

  function downloadImage() {
    const imageElement = imageContainer.find(&#39;img&#39;)[0];
    const link = $(&#39;&lt;a&gt;&#39;).attr(&#39;href&#39;, imageElement.src).attr(&#39;download&#39;, &#39;cool_font.png&#39;)[0];
    link.click();
  }

  function copyText() {
    const transformedText = outputText.text();
    navigator.clipboard.writeText(transformedText)
      .then(() =&gt; {
        alert(&#39;Text copied to clipboard!&#39;);
      })
      .catch((error) =&gt; {
        console.error(&#39;Error copying text to clipboard:&#39;, error);
      });
  }
});

<!-- language: lang-css -->

body {
  text-align: center;
  margin-top: 100px;
}

#inputText {
  font-size: 24px;
  padding: 10px;
}

#outputText {
  font-family: &quot;Courier New&quot;, Courier, monospace;
  font-size: 48px;
  margin-top: 20px;
}

#imageContainer {
  margin-top: 20px;
}

.button {
  margin-top: 20px;
}

.option {
  margin-top: 10px;
}

<!-- language: lang-html -->

&lt;h1&gt;Cool Font Generator&lt;/h1&gt;
  &lt;input type=&quot;text&quot; id=&quot;inputText&quot; placeholder=&quot;Enter text&quot;&gt;
  &lt;div id=&quot;outputText&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;option&quot;&gt;
&lt;label for=&quot;fontSize&quot;&gt;Font Size:&lt;/label&gt;
&lt;input type=&quot;number&quot; id=&quot;fontSize&quot; min=&quot;10&quot; max=&quot;100&quot; value=&quot;48&quot;&gt;
  &lt;/div&gt;
  &lt;div class=&quot;option&quot;&gt;
&lt;label for=&quot;fontColor&quot;&gt;Font Color:&lt;/label&gt;
&lt;input type=&quot;color&quot; id=&quot;fontColor&quot; value=&quot;#FFFFFF&quot;&gt;
  &lt;/div&gt;
  &lt;div class=&quot;option&quot;&gt;
&lt;label for=&quot;backgroundColor&quot;&gt;Background Color:&lt;/label&gt;
&lt;input type=&quot;color&quot; id=&quot;backgroundColor&quot; value=&quot;#000000&quot;&gt;
  &lt;/div&gt;
  &lt;div class=&quot;option&quot;&gt;
&lt;label for=&quot;fontWeight&quot;&gt;Font Weight:&lt;/label&gt;
&lt;select id=&quot;fontWeight&quot;&gt;
  &lt;option value=&quot;normal&quot;&gt;Normal&lt;/option&gt;
  &lt;option value=&quot;bold&quot;&gt;Bold&lt;/option&gt;
&lt;/select&gt;
  &lt;/div&gt;

  &lt;div id=&quot;imageContainer&quot;&gt;&lt;/div&gt;

  &lt;button class=&quot;button&quot; id=&quot;downloadButton&quot;&gt;Download Image&lt;/button&gt;
  &lt;button class=&quot;button&quot; id=&quot;copyButton&quot;&gt;Copy Text&lt;/button&gt;

  &lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 1

如评论中由 @CBroe 建议的,您可以添加 min-height(或 height,如果您希望它保持一行宽度):

/* ... 您的 CSS 代码 ... */
#outputText, #imageContainer {
  min-height: 55px; /* 或者使用 height: 55px; */
  border: 1px solid black;
}

这是它的样子:

如何在页面加载时同时显示带有空白框或已定义字母预览的两个 div?

英文:

As suggested by @CBroe in the comments, you can add min-height (or height, if you want it to stay one line wide):

<!-- language: lang-css -->

/* ... your css here ... */
#outputText, #imageContainer {
  min-height: 55px; /* or use height: 55px; */
  border: 1px solid black;
}

Here's what it looks like:

如何在页面加载时同时显示带有空白框或已定义字母预览的两个 div?

huangapple
  • 本文由 发表于 2023年6月27日 18:25:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定